在SELECT语句中在GROUP BY之前处理WHERE。如何在COUNT(名称)的结果上使用WHERE?
我想要的是:
SELECT topic, COUNT(name) AS counter
FROM blah
GROUP BY name
WHERE counter <> 1
答案 0 :(得分:13)
SELECT topic, COUNT(name) AS counter
FROM blah
GROUP BY topic
HAVING COUNT(name) <> 1
答案 1 :(得分:7)
我认为你正在寻找有条款:
http://msdn.microsoft.com/en-us/library/ms180199.aspx
SELECT topic, COUNT(name) AS counter
FROM blah
GROUP BY topic
HAVING COUNT(name) <> 1
答案 2 :(得分:3)
正如对方已经回答你需要一个人。
WHERE
过滤所有联接后剩余的行
GROUP BY
将行组合成组
HAVING
过滤这些群组
不要担心在SELECT列表中重复COUNT(*)并且有,优化器足够智能以优化大多数数据库的这个