Delete - I can't specify target table?

markzzz picture markzzz · Apr 28, 2011 · Viewed 29.2k times · Source

Why this query doesn't work?

DELETE FROM recent_edits 
WHERE trackid NOT IN 
     (SELECT DISTINCT history.trackid 
     FROM history JOIN recent_edits ON history.trackid=recent_edits.trackid 
     GROUP BY recent_edits.trackid)

I get this message : "You can't specify target table "recent_edits" for update in FROM clause

Answer

Nicola Cossu picture Nicola Cossu · Apr 28, 2011

Try in this way

DELETE FROM recent_edits 
WHERE trackid NOT IN 
     (select * from (SELECT DISTINCT history.trackid 
     FROM history JOIN recent_edits ON history.trackid=recent_edits.trackid 
     GROUP BY recent_edits.trackid) as t);