MySQL——排序操作与聚集函数

栏目: 数据库 · 发布时间: 6年前

内容简介:order by,将查询结果进行排序,放在select子句的最后说明order by 子句是比select子句后执行的。

order by,将查询结果进行排序,放在select子句的最后

asc ---> 升序(默认)

desc---> 降序

单列排序

/* 年龄按照降序排序 */
 select * from t_students order by age desc;

别名使用单引号,就不能 排序 了!!!

多列排序

/* 首先对id进行升序排序,再对年龄进行降序排序 */
  select * from t_students order by id ,age desc;

MySQL——排序操作与聚集函数

列的别名排序

select *,id+age as XX from t_students order by XX desc;

说明order by 子句是比select子句后执行的。

聚集函数

作用于一组数据,并对一组数据返回一个值

名称 作用
count 统计结果记录数
max 统计计算最大值
min 统计计算最小值
sum 统计计算求和
avg 统计计算平均值
/* 求一列的平均数 */
select avg(age) from tablename;
/* 求id=2的总数 */
select count(id) from tablename where id=2;
/* 求商品的最高价、最低价、平均价 */
select max(saleprice),min(saleprice),avg(saleprice) from tablename;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Is Parallel Programming Hard, And, If So, What Can You Do About

Is Parallel Programming Hard, And, If So, What Can You Do About

Paul E. McKenney

The purpose of this book is to help you understand how to program shared-memory parallel machines without risking your sanity.1 By describing the algorithms and designs that have worked well in the pa......一起来看看 《Is Parallel Programming Hard, And, If So, What Can You Do About 》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器