2012-07-06 53 views
0

我有一個有數千行的巨大表格。每一行都有獨特的路徑。例如:SQL查看所有重複項?

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查詢可以做到這一點?

+3

看到:: HTTP://stackoverflow.com/questions/ 688549/found-duplicate-values-in-mysql – 2012-07-06 12:31:12

+0

@Sudhir:這並不完全符合OP的要求,因爲隨後需要重新擴展組。 – eggyal 2012-07-06 12:35:19

回答

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:[email protected]+1); 

看到它的sqlfiddle

+0

真棒...有沒有辦法更新所有重複項,以便路徑以「-1」,「-2」,「-3」等結尾......? – coderama 2012-07-06 12:39:03

+0

@RD .:看到我上面更新的答案。 – eggyal 2012-07-06 12:42:44

+0

哇。我從來沒有見過這種符號。令人印象深刻的... – coderama 2012-07-06 12:53:03

0

試試這個,替代所需要的表和列值:

SELECT YourColumn, COUNT(*) TotalCount 
    FROM YourTable 
GROUP BY YourColumn 
HAVING COUNT(*) > 1 
ORDER BY COUNT(*) DESC 
0

您可以通過使用這樣的查詢看到所有重複

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

你想要什麼樣的行刪除?

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;