Find duplicates in the same table in MySQL

后端 未结 13 1899
感动是毒
感动是毒 2020-12-10 02:03

I have a table with two columns - artist, release_id

What query can I run to show duplicate records?

e.g. my table is

ArtistX : 45677
ArtistY         


        
13条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 02:21

    This method might not be great for you, but if you ever want to get rid of duplicates and do this while making sure they are duplicates indeed you can try this:

    1. duplicate your table1 into table2, for example like this:

      CREATE TABLE table2 AS SELECT * FROM table1;
      
    2. add a new column to table1, for example, name it to count

    3. run a query (this assumes release_id should an unique column):

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

    2. use table1.kount to find your duplicates and remove them or something. Preferably in PHP/Python/Perl. This way you can, for example, make sure they are indeed duplicates and just have the same release_id. The same release_id might be given by accident and titles, years of publication, etc. might be different. So just put your code here to filter the duplicates (pseudocode):

    foreach (sql(SELECT * FROM table1 WHERE kount>1)) do
        //do something
    

提交回复
热议问题