如何对GROUP BY子句生成的COUNT(名称)执行WHERE?

时间:2010-04-12 06:04:52

标签: sql

在SELECT语句中在GROUP BY之前处理WHERE。如何在COUNT(名称)的结果上使用WHERE?

我想要的是:

SELECT topic, COUNT(name) AS counter
FROM blah
GROUP BY name
WHERE counter <> 1

3 个答案:

答案 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(*)并且有,优化器足够智能以优化大多数数据库的这个