id | teach_id | name | age |
---|---|---|---|
1 | 1 | 小红 | 13 |
2 | 1 | 小清 | 14 |
3 | 2 | 小d | 11 |
4 | 1 | 小1 | 13 |
5 | 1 | 小2 | 14 |
6 | 2 | 小a | 11 |
不使用having
SELECT * FROM (SELECT teach_id, AVG(age) AS age FROM student GROUP BY teach_id) t WHERE t.age > 12;
使用having
SELECT teach_id, AVG(age) AS age FROM student GROUP BY teach_id HAVING AVG(age) > 12
having可以为分组设置条件