47

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

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
markzzz
  • 47,390
  • 120
  • 299
  • 507

2 Answers2

133

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);
Nicola Cossu
  • 54,599
  • 15
  • 92
  • 98
37

You can't post-process a table which is locked for deletion. using the hack select * from (query) as Nicola states will generate a temporary table instead of direct access.

Edit - make sure that you give ID to the tables you use since it is nested and will require uniqueID for every table.

Elysiumplain
  • 711
  • 8
  • 21