Remove all duplicates from database except for the first

E.g. lets say you have ‘table’ with column ‘slug’ and then there are 3 records with the same slug value and you want to remove 2nd, 3rd but keep the 1st.

select * FROM table a INNER JOIN table b ON a.slug = b.slug WHERE a.id > b.id;

You create an inner join on the same table – joining them by the column that the row duplicates are based on – in this case, column ‘slug’. What this select outputs are exactly the rows you wish to delete – 2nd and 3rd row.

Because in WHERE clause we specify that only those rows that have ID higher than the first occurrence.

Now to actually delete these records, you just put a delete in front, e.g.

delete from table where id IN (select id FROM table a INNER JOIN table b ON a.common_column = b.common_column WHERE a.id > b.id);

Write a Comment

Comment

Webmentions

  • generické kamagra spojené království

    nejlepší cena na kamagra

    levná obecná kanada kamagra

  • cheapest buy xifaxan for sale usa

    buy xifaxan generic good

    buying xifaxan cheap online in the uk

  • how to order avodart price south africa

    how to buy avodart generic from canadian pharmacy

    no prescription avodart overnight

  • am a womani have emphysema low hemo etc dr wants to prescribe staxyn

    cheap staxyn uk suppliers

    ordering staxyn usa cheap

  • buying itraconazole generic is good

    buy cheap itraconazole without prescription

    purchase itraconazole generic how effective

  • No perscription needed fildena

    fildena canada pharmacy

    ordering fildena buy online no prescription

  • gabapentin without a perscription or

    buy gabapentin cost at costco

    online order gabapentin purchase usa

  • discount flexeril cyclobenzaprine purchase in canada

    cheapest buy flexeril cyclobenzaprine us pharmacies

    cheapest buy flexeril cyclobenzaprine usa pharmacy

  • discount dutasteride cheap now

    purchase dutasteride generic release date

    how to buy dutasteride generic uk

  • enclomiphene prices

    enclomiphene with free dr consultation

    no prescription enclomiphene with fedex

  • je veux commander kamagra sans ordonnance

    kamagra livraison fedex

    generique kamagra en suisse acheter

  • canadian pharmacy rifaximin

    rifaximin no prescription usa fedex shipping

    rifaximin online no perscription overnight

  • ordering androxal cheap wholesale

    how to buy androxal us pharmacies

    how to buy androxal cheap pharmacy