Removing Duplicate Data from a Table using MySQL
-
21-04-2021 - |
Question
I am trying to remove duplicate data from my database. I found a nice example on here of how to do this on an oracle database.
The bottom query from that answer (only selecting the duplicate rows) works in MySQL, but the delete query (see below) does not...
"DELETE FROM studios as a
WHERE a.id >
ANY (SELECT b.id
FROM studios as b
WHERE a.name = b.name
AND a.email = b.email
)"
The error I get is:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'a
WHERE a.id >
ANY (SELECT b.id
FROM studios as b
' at line 1
So, I had a look for the right delete syntax and any syntax to use, but couldn't find anything wrong with my query... Any ideas?
Solution
Try this query -
DELETE t1 FROM studios t1
JOIN (SELECT MIN(id) id, name, email FROM studios GROUP BY name, email) t2
ON t1.id <> t2.id AND t1.name = t2.name AND t1.email = t2.email;
OTHER TIPS
The AS
operator doesn't work with DELETE
statements in MySQL.
Try this (unverified):
DELETE FROM a using studios a
WHERE a.id >
ANY
(
SELECT b.id
FROM studios as b
WHERE a.name = b.name
AND a.email = b.email
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow