最近遇到几个以前没曾注意到的sql问题,今天先记下来,如果以后再遇到了,再继续补
1.group by后面的字段必须也作为查询的字段之一,否则报错,正确格式如下:
查询出每个学科及格的学生姓名
select name,subject from stu where score>60 group by subject
2.where后面不能使用聚合函数,否则报错
查询所有学科都及格的学生姓名
错误格式如下:select name from stu where min(score>60) group by name
这样写会报错,正确格式如下
select name from stu group by name having min(score>60)