I’m trying to delete duplicate rows in a table. The email field needs to be be unique in the table for the code after it to run. Here’s my code:
DELETE FROM deleteRequests
WHERE Email NOT IN (
SELECT MAX(Email)
FROM deleteRequests
GROUP BY email);
The error I get is: ‘#1093 – Table ‘deleteRequests’ is specified twice, both as a target for ‘DELETE’ and as a separate source for data’
I think the problem is that I’m using mariadb 10.2 (which also means I can’t use WITH CTE). I am not in control of updating this, so I need a workaround. Suggestions?
2
Answers
I ended up creating a new table to work from.
This gave me the table I needed, and so instead of trying to get it to update my original table by using a
LEFT JOIN
andDELETE
, I just used it. They're both temporary tables that get deleted at the end of the process, so it works.MySQL, MariaDB doesn’t allow the table to be referenced again in a
DELETE
orUPDATE
. An easy work-around is to use aJOIN
:Of course, this query is non-sensical because you are aggregating by
email
and choosing the max — so all non-NULL
emails will match.Perhaps you want
HAVING COUNT(*) = 1
in the subquery,.