1 排序
查询学生,先按年龄升序,再按照成绩降序
select * from student order by age asc,score desc;
2 聚合函数
查询学生总人数
select count(*) as 总人数 from student;
如果某一位置为null
,可以使用IFNULL()函数
查询学生总人数,某个位置为空,给它默认值
select count(IFNULL(id,0)) from student;
3 分组
按照性别进行分组,统计男女成绩平均分
select gender ,AVG(score) from student group by gender;
当我们使用某个字段分组,查询也要将这个字段查出来,不然看不到数据属于哪组,分组有什么意义呢
统计每一组人数,按照性别分组
select gender,count(id) from student group by gender;
查询年龄大于25岁,按照性别分组,统计每组人数,并只显示性别大于2的组
select gender,count(*) from student where age>25 group by gender having count(*) >2;
代码块
1.查询岗位名以及岗位包含的所有员工名字
select job_name ,GROUP_CONCAT(name) from employee GROUP BY job_name;
2.查询平均薪水大于10000的岗位及岗位平均薪资
select job_name, AVG(salary) from employee GROUP BY job_name having avg(salary)>10000;
3.查询平均薪水大于10000的岗位和岗位平均薪资,并按照岗位薪资降序排列
select avg(salary) ,job_name from employee GROUP BY job_name HAVING avg(salary)>10000 ORDER BY avg(salary) DESC;
4.查询雇员表所有信息,先按照年龄升序排列,再按照id降序排列
select * from employee ORDER BY age asc,id desc;
4 limit语句、用于分页
查询学生数据,从第三条开始,显示6条
select * from student limit 2,6;
原文地址:https://www.cnblogs.com/hellosiyu/p/12501641.html
时间: 2024-10-18 12:44:17