SQL中Having與Where的區別

含義

  • where
    在分組以前就進行選擇操做sql

  • 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
相關文章
相關標籤/搜索