SQL中Having与Where的区别

含义

  • where
    在分组之前就进行选择操作

  • having
    having在分组操作之后才进行选择操作,所以having可以选择聚合函数

eg:

// 下面这句是可以的
SELECT COUNT(*) FROM A
WHERE A.a1>0
GROUP BY a2;
//但是下面就不行
SELECT COUNT(*) FROM A
WHERE A.a1>0 and count(*)>1
GROUP BY a2;
//必须要改为having
SELECT COUNT(*) FROM A
WHERE A.a1>0
GROUP BY a2
HAVING COUNT(*)>1
    原文作者:Cesar
    原文地址: https://segmentfault.com/a/1190000004358398
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞