查找重复的名称sql

时间:2012-07-30 14:21:44

标签: sql duplicates

SELECT COUNT(organization.ID) 
FROM organization 
WHERE name in ( SELECT name FROM organization GROUP BY name HAVING count( name ) >1 )
AND organization.APPROVED=0 
AND organization.CREATED_AT>'2010-07-30 10:30:21'

我正在尝试查找重复项,但此查询需要花费很长时间大约5-6秒。有没有其他方法我可以找到重复项而不使用我的方法?感谢。

子查询:0.28秒。一切都是5.98秒。

3 个答案:

答案 0 :(得分:1)

SELECT organization.name, COUNT(organization.ID)
  FROM organization
 WHERE organization.APPROVED=0
   AND organization.CREATED_AT>'2010-07-30 10:30:21'
 GROUP BY name
HAVING count(organization.id) > 1;

答案 1 :(得分:0)

无需在WHERE子句中使用该查询。您可以使用GROUP BYHAVING子句来完成此任务:

SELECT COUNT(o.ID) 
FROM organization o
WHERE o.APPROVED=0 
   AND o.CREATED_AT>'2010-07-30 10:30:21'
GROUP BY o.Name
HAVING COUNT(o.name) > 1

答案 2 :(得分:0)

为什么不这样做:

SELECT COUNT(organization.ID)
FROM organization 
WHERE organization.APPROVED=0 
AND organization.CREATED_AT>'2010-07-30 10:30:21'
GROUP BY organization.name
HAVING count(organization.name) > 1;