I am trying to write my first mySQL query. I need to delete rows if they have the same article-number field. I wrote this query:
SELECT article_number, COUNT(*) FROM article_numbers GROUP BY article_number HAVING COUNT(*) > 1 It shows me all the rows that are duplicate. But how can I delete all but 1 for each duplicate?
Thanks
EDIT:
I tried this query:
delete article_numbers from article_numbers inner join (select article_number from article_numbers group by article_number having count(1) > 1) as duplicates on (duplicates.article_number = article_numbers.article_number) but it gives me this error:
Cannot delete or update a parent row: a foreign key constraint fails (
api.products, CONSTRAINTproducts_article_number_id_foreignFOREIGN KEY (article_number_id) REFERENCESarticle_numbers(id))
EDIT 2:
I disabled the foreign key temporarily, and now my delete query works. But how can I modify it that one of the duplicate rows is not deleted?
article_numbers?