查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
--解法一:
select t1.s_id, t2.s_name, avg(t1.s_score) as avg_score from score t1 left join student t2 on t1.s_id = t2.s_id group by t1.s_id having avg(t1.s_score) > 60
--参考解法:
select b.s_id, b.s_name, round(avg(a.s_score), 2) as avg_score from student b join score a on b.s_id = a.s_id group by b.s_id, b.s_name having round(avg(a.s_score), 2)>= 60;
GROUP BY表示根据哪个字段进行分组
GROUP BY必须得配合聚合函数来用,分组之后可以使用聚合函数。
常用聚合函数
count() 计数 sum() 求和 avg() 平均数 max() 最大值 min() 最小值 HAVING相当于条件筛选,但它与WHERE筛选不同,HAVING是对于GROUP BY对象进行筛选。--2019/04/16