题解 | #分组过滤练习题#
分组过滤练习题
https://www.nowcoder.com/practice/ddbcedcd9600403296038ee44a172f2d
SELECT university, round(avg(question_cnt),3) as avg_question_cnt, round(avg(answer_cnt),3) as avg_answer_cnt from user_profile group by university having avg_question_cnt<5 or avg_answer_cnt<20
这里不适用where的原因是因为:where先于avg()/sum()执行,那时候还没有计算出平均值,就会出错。
报错信息:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE avg_question_cnt<5 or avg_answer_cnt<20' at line 6