SQL中Having与Where的区别
含义
where
在分组之前就进行选择操作
having
having在分组操作之后才进行选择操作,所以having可以选择聚合函数
eg:
1
2
3
4
|
// 下面这句是可以的
SELECT COUNT (*) FROM A
WHERE A.a1>0
GROUP BY a2;
|
1
2
3
4
|
//但是下面就不行
SELECT COUNT (*) FROM A
WHERE A.a1>0 and count (*)>1
GROUP BY a2;
|
1
2
3
4
5
|
//必须要改为 having
SELECT COUNT (*) FROM A
WHERE A.a1>0
GROUP BY a2
HAVING COUNT (*)>1
|
本文由主机测评网发布,不代表主机测评网立场,转载联系作者并注明出处:https://zhuji.jb51.net/shujuku/2931.html