mysql常用查询:group by,左连接,子查询,having where 前几天去了两个比较牛的互联网公司面试,在sql这块都遇到问题了,哎,可惜呀,先把简单的梳理一下 成绩表 score 1、group by 使用 按某一个维度进行分组 例如: 求每个同学的总分 select student,sum(
mysql常用查询:group by,左连接,子查询,having where前几天去了两个比较牛的互联网公司面试,在sql这块都遇到问题了,哎,可惜呀,先把简单的梳理一下
成绩表 score
1、group by 使用按某一个维度进行分组
例如:
求每个同学的总分
select student,sum(score) from score group by student
求每个同学的平均分
select student,avg(score) from score group by student
也可以按照 班级,课程 来求
2、having 与 where的区别 having与where类似,可以筛选数据,where后的表达式怎么写,having后就怎么写
- where针对表中的列发挥作用,查询数据
- having对查询结果中的列发挥作用,筛选数据
查出挂了两门及以上的学生
select student,sum(score<60)as gk from score group by student having gk>1
3、子查询(1)where子查询
(把内层查询结果当作外层查询的比较条件)
求比每门课程平均分低的学生
select student ,course, score
from score ,(select course as a_course,avg( score)as a_score from score group by course) as avg_score
where course = a_course and score
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!