MySQL #1093 - You can't specify target table 'giveaways' for update in FROM clause

Eray picture Eray · Nov 30, 2011 · Viewed 49.7k times · Source

I tried:

UPDATE giveaways SET winner = '1' WHERE ID = (SELECT MAX(ID) FROM giveaways)

But it gives:

#1093 - You can't specify target table 'giveaways' for update in FROM clause

This article seems relevant but I can't adapt it to my query. How can I get it to work?

Answer

ipr101 picture ipr101 · Nov 30, 2011

Based on the information in the article you linked to this should work:

update giveaways set winner='1'
where Id = (select Id from (select max(Id) as id from giveaways) as t)