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

Why I choose her?

Are you an Introvert?

I Am Getting Married!