在MySQL的同一个表中查找重复项

时间:2012-02-11 11:15:12

标签: mysql

我有一个包含两列的表格 - artist,release_id

我可以运行哪些查询来显示重复记录?

e.g。我的桌子是

ArtistX : 45677
ArtistY : 378798
ArtistX : 45677
ArtistZ : 123456
ArtistY : 888888
ArtistX : 2312
ArtistY: 378798

查询应显示

ArtistX : 45677
ArtistX : 45677
ArtistY : 378798
ArtistY : 378798

13 个答案:

答案 0 :(得分:43)

如果存在重复项,您可以在感兴趣的列中使用分组。

SELECT
    artist, release_id, count(*) no_of_records
FROM table
GROUP BY artist, release_id
HAVING count(*) > 1;

答案 1 :(得分:4)

SELECT id,artist,COUNT(*) FROM myTable
GROUP BY artist, release_id HAVING COUNT(*) > 1

答案 2 :(得分:2)

你可以尝试这样的事情

select artist, count(*) from mytable group by artist having count(*) > 1;

将输出

artist   count(*)
45677    2
378798   2

答案 3 :(得分:2)

SELECT row, COUNT(row) AS num FROM mytable GROUP BY row HAVING (num > 1);

答案 4 :(得分:2)

选择     artist,release_id,count(*)no_of_records,group_concat(id) 从表 GROUP BY艺术家,release_id 有计数(*)> 1;

同时添加group_concat(id)可以获得重复项的所有ID。

答案 5 :(得分:1)

您可以将此查询用于相同的结果。它对我有用

SELECT firstname,lastname,list.address FROM list INNER JOIN(选择地址FROM列表 GROUP BY地址HAVING count(id)> 1)dup ON list.address = dup.address

答案 6 :(得分:1)

从艺术家IN的表格中选择*(从具有计数(ID)> 1的艺术家中选择表格组中的艺术家)和release_id IN(通过具有计数的release_id从表格组中选择release_id(release_id)> 1);

将获取: ArtistX:45677 ArtistX:45677 ArtistY:378798 ArtistY:378798

答案 7 :(得分:0)

SELECT id,artist,COUNT(id) as found FROM table GROUP by id HAVING found > 1

答案 8 :(得分:0)

SELECT artist, count(*) 
FROM tableName 
GROUP BY artist 
HAVING count(*) > 1;

答案 9 :(得分:0)

试试这个:

SELECT A.ARTIST,A.RELEASE_ID FROM ARTISTS A
WHERE EXISTS(
SELECT 'X' FROM ARTISTS B
WHERE B.ARTIST = A.ARTIST AND B.RELEASE_ID = A.RELEASE_ID
GROUP BY B.ARTIST,B.RELEASE_ID
HAVING COUNT(B.ARTIST)>1)
ORDER BY A.ARTIST;

答案 10 :(得分:0)

这种方法对你来说可能不是很好,但是如果你想要摆脱重复并做到这一点,同时确保它们是重复的,你可以试试这个:

  1. 将您的table1复制到table2,例如:

    CREATE TABLE table2 AS SELECT * FROM table1;

  2. table1添加新列,例如将其命名为kount

  3. 运行查询(这假定release_id应该是唯一的列):

    UPDATE table1 AS t1 SET t1.kount =(SELECT COUNT(*)FROM table2 AS t2 WHERE t1.release_id = t2.release_id)

  4. drop table table2

  5. 使用table1。kount查找重复项并删除它们。最好是PHP / Python / Perl。例如,通过这种方式,您可以确保它们确实是重复的,并且只具有相同的release_id。同样的release_id可能是偶然发生的,标题,出版年限等可能会有所不同。所以只需将您的代码放在此处以过滤重复项(伪代码):

    foreach(sql(SELECT * FROM table1 WHERE kount> 1))do //做点什么

答案 11 :(得分:0)

您还可以尝试以下操作:

SELECT W.artist, W.release_id FROM table W, table W1 
WHERE W.artist= W1.artist AND W.release_id = W1.release_id;

答案 12 :(得分:-2)

如果您在一行中有更多唯一列,则可以使用:

subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
subsystem: com.apple.UIKit, category: HIDEventIncoming, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
subsystem: com.apple.BaseBoard, category: MachPort, enable_level: 1, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0, enable_private_data: 0
subsystem: com.apple.UIKit, category: StatusBar, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
subsystem: com.apple.libsqlite3, category: logging, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
subsystem: com.apple.SystemConfiguration, category: SCPreferences, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 2, enable_private_data: 0