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;

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

查看所有标签

猜你喜欢:

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

Open Data Structures

Open Data Structures

Pat Morin / AU Press / 2013-6 / USD 29.66

Offered as an introduction to the field of data structures and algorithms, Open Data Structures covers the implementation and analysis of data structures for sequences (lists), queues, priority queues......一起来看看 《Open Data Structures》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具