Delete Duplicate Values but Retain One in MySQL

I'm not good at explaining so, let's just demonstrate it. Say for example (although this is some sort of explanation) we have this table in our database:

Table Name: tag

NAME ID
test 1
test 2
test 3
again 4
again 5
nodup 6


with this query,

DELETE a FROM tag a
LEFT JOIN
(
    SELECT MIN(id) AS id, name
    FROM tag
    GROUP BY name
) b ON a.id = b.id AND a.name = b.name
WHERE b.id IS NULL;


...the result will be

NAME ID
test 1
again 4
nodup 6

Popular posts from this blog

Display Post View Count: The Easiest Way

Style Your Blog Posts Elements Separately

Romantic Movies that involve Time Travel