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

前端 未结 6 1004
小蘑菇
小蘑菇 2020-11-28 12:09

I tried:

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

But it gives:

#1093 - Yo

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 12:26

    This is because your update could be cyclical... what if updating that record causes something to happen which made the WHERE condition FALSE? You know that isn't the case, but the engine doesn't. There also could be opposing locks on the table in the operation.

    I would think you could do it like this (untested):

    UPDATE
        giveaways
    SET
        winner = '1'
    ORDER BY
        id DESC
    LIMIT 1
    

    Read more

提交回复
热议问题