数据库聚合函数

来源:互联网 发布:大学生炒股知乎 编辑:程序博客网 时间:2024/06/15 17:36

聚合函数
count 用来统计一共有多少条记录
select count(id) from students;
sum 用来计算和
select sum(score) from students;
max 用来求最大值
select max(score) from students;
min 用来求最小值
select min(score) from students;
avg 用来求平均值
select avg(score) from students;
这里还可以组合使用   as  起别名:
select count(id) as 总条数,sum(score) as 总和,max(score) as 最大值, min(score) as 最小值, avg(score) as 平均值 from students;
order by 对查询结果进行排序,asc 升序,desc.
可以多字段查询,在前边的优先级高,
一个小例子,成绩降序,成绩一样,年龄升序
select score,age from students order by score desc,age asc;
limit 指定查询记录多少条
select score,age from students order by score desc,age asc limit 3;
也可以这样使用,从第几条开始。这就是从第二条开始
select score,age from students order by score desc,age asc limit 2,3;

原创粉丝点击