sql语法中group by 和having子句,用select嵌套查询来替代
擅用嵌套查询
1)group by是分组聚合函数,与having连用,having是对分组计算后的字段进行筛选,必须与group by连用。
2)where子句是对from中的数据进行过滤,where中的字段必须是from中的字段,当要过滤的字段不在from中时而在select中时,可以用select嵌套。
如:
select
T.name,T.avg_score
from
(select name,avg(score) avg_score
from a
group by name
) T
where
T.avg_score > 60;
等同于
select name,avg(score) as avg_score
from a
group by name
having avg_score > 60;