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

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

内容简介: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;

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

查看所有标签

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

Web Standards Creativity

Web Standards Creativity

Andy Budd、Dan Rubin、Jeff Croft、Cameron Adams、Ethan Marcotte、Andy Clarke、Ian Lloyd、Mark Boulton、Rob Weychert、Simon Collison、Derek Featherstone / friends of ED / March 19, 2007 / $49.99

Book Description * Be inspired by 10 web design lessons from 10 of the world's best web designers * Get creative with cutting-edge XHTML, CSS, and DOM scripting techniques * Learn breathtakin......一起来看看 《Web Standards Creativity》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

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

在线压缩/解压 CSS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码