0

我有一张有数千行的大桌子。每一行都有一个唯一的路径。例如:

electronics/samsung/tv/lcd-23384
electronics/philips/tv/lcd-12ger
etc...

问题是,最近的查询执行不正确,导致某些字段具有重复的路径。所以,我想知道的是,有没有可以执行的查询来显示所有重复项?换句话说,如果我有这个:

ID     | PATH 
1      | path_1
2      | path_2
3      | path_3
4      | path_3
5      | path_3
6      | path_4
7      | path_4
8      | path_5
9      | path_6

我想要这个结果:

ID     | PATH 
3      | path_3
4      | path_3
5      | path_3
6      | path_4
7      | path_4

请注意,所有非重复项已被删除。即....这些:

ID     | PATH 
1      | path_1
2      | path_2
8      | path_5
9      | path_6

什么 SQL 查询可以做到这一点?

4

4 回答 4

4
SELECT * FROM mytable NATURAL JOIN (
  SELECT PATH FROM mytable GROUP BY PATH HAVING COUNT(*) > 1
) dupes

sqlfiddle上查看。


要执行您在以下评论中请求的更新:

UPDATE mytable NATURAL JOIN (
  SELECT PATH FROM mytable GROUP BY PATH HAVING COUNT(*) > 1
) dupes, (SELECT @r:=0) init
SET mytable.PATH = CONCAT(PATH, '-', @r:=@r+1);

sqlfiddle上查看。

于 2012-07-06T12:32:04.673 回答
0

试试这个,替换所需的表和列值:

SELECT YourColumn, COUNT(*) TotalCount
  FROM YourTable
 GROUP BY YourColumn
HAVING COUNT(*) > 1
 ORDER BY COUNT(*) DESC
于 2012-07-06T12:34:24.140 回答
0

您可以使用此类查询查看所有重复项

SELECT * FROM YourTable WHERE Path in
  (SELECT PATH
   FROM YourTable
   GROUP BY PATH
   HAVING COUNT(*)>1)

你想删除什么样的行?

于 2012-07-06T12:35:10.450 回答
0

它会给你所有重复的结果

select column_name, count(column_name) occurrence from table_name 
group by column_name having count(column_name) > 1;

如果你想得到所有不重复的结果..

select column_name, count(column_name) occurrence from table_name 
group by column_name having count(column_name) = 1;
于 2012-07-06T12:41:48.140 回答