I have a MySQL database table that contains an Article ID ( primary key ) and an Article Title. I want to remove duplicate titles from the table, but keep the first occurrence of the title. I initially simply did a query for all duplicate titles:
SELECT
title,
count( id ) AS count
FROM articles
GROUP BY title
HAVING count > 1
Then I replaced all the duplicate titles with a blank using a foreach loop and this command:
UPDATE articles
SET title = ''
WHERE title = '$duplicate_title'
I'd like to update the articles
table and replace all duplicate titles except the first entry, based on the Article ID ASC using something like this. The problem is that OFFSET doesn't seem to work in an UPDATE. Is there a way to do this in a single query?
UPDATE articles
SET title = ''
WHERE title = '$duplicate_title'
ORDER BY id ASC
OFFSET 1
UPDATE articles a
INNER JOIN articles b
ON a.title = b.title AND a.ID > b.ID
SET title = '';
This basically says
update all articles where there exists a matching article with the same title and a lower ID
I found another solution that was a little outside the scope of my original question, but relevant nonetheless.
I already had a count of duplicates from the first query that found them. I subtracted one from this count, then ordered my UPDATE query by ID DESC and then LIMITed the query to the count minus one. This serves the same purpose and removes all duplicates except for the first entry.
Here is the UPDATE query I use:
UPDATE articles
SET title = ''
WHERE title = '$duplicate_title'
ORDER BY id DESC
LIMIT $duplicate_count_minus_one
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With