16

请建议解决我的问题的最佳方法。

我在弄清楚如何计算下表中的重复项时遇到问题

Street       | City

avenue 123   | New York
avenue 123   | New York
avenue 20    | New York
avenue 35    | Chicago
avenue 12    | Chicago
avenue 123   | Chicago
avenue 12    | Chicago
avenue 12    | Chicago

我想在同一个城市有多个重复的街道,结果如下?

结果:

Street     | City | Duplicates

avenue 123 | New York | 2
avenue 12  | Chicago | 3
4

2 回答 2

26

使用GROUP BY,COUNTHAVING:

 SELECT Street, City, COUNT(*)
 FROM yourtable
 GROUP BY Street, City
 HAVING COUNT(*) > 1

在线查看它:sqlfiddle

于 2012-12-19T12:30:56.723 回答
5

尝试 :

SELECT street, city, COUNT(*) AS duplicates
FROM yourtable
GROUP BY street, city
HAVING COUNT(*) >1

HAVING COUNT(*) > 1如果您还想显示没有重复的行,请删除。

于 2012-12-19T12:31:06.677 回答