Your query is correct and will work on other DBMSs, but MySQL does not allow you to update or delete from a table and select from the same table in a subquery. It is documented on the official DELETE docs.
It may be fixed for future releases, but your request is currently not supported.
A simple solution would be to put your subquery in a subquery, as in echo_Me's answer:
WHERE NAME.id NOT IN (SELECT * FROM (your subquery) s)
this will force MySQL to create a temporary table with the results of your subquery, and since you are not actually selecting the same table, but from the temporary table, this query will work fine. However, performance may be poor.
You usually get rid of error # 1093 using unions. This is your request in the form of a connection:
DELETE NAME FROM NAME LEFT JOIN ( SELECT col1, col2, MIN(id) min_id FROM NAME GROUP BY col1, col2) s ON (NAME.col1, NAME.col2, NAME.id) = (s.col1, s.col2, s.min_id) WHERE s.min_id IS NULL
or you can use this simpler version, which should be the fastest:
DELETE N1 FROM NAME N1 INNER JOIN NAME N2 ON N1.col1=N2.COL1 AND N1.col2=N2.col2 AND N1.ID > N2.ID
Fiddle here .
fthiella
source share