MySQL group by 注意事项
有学生表和成绩表,筛选出选修超过三门课的学生信息,并按照总分排序。
如果使用where筛选count()计算结果会报错,原因是where发生在group by之前,需要使用having进行筛选。
构造的查询语句:
select SNAME,SUM(GRADE) as sum,STU.STU_ID as count from STU right join SEL on STU.STU_ID = SEL.STU_ID group by STU.STU_ID having count(SNAME)>3 order by sum desc;