You can’t specify target table ‘xx_themes’ for update in FROM clause

1、执行 SQL 如下

UPDATE `xx_themes` SET `is_original` = (SELECT `is_original` FROM `xx_themes` WHERE `id` = '237') WHERE `id` = 248

2、报错:1093 – You can’t specify target table ‘xx_themes’ for update in FROM clause。如图1

图1

UPDATE `xx_themes` SET `is_original` = (SELECT `is_original` FROM `xx_themes` WHERE `id` = '237') WHERE `id` = 248
> 1093 - You can't specify target table 'xx_themes' for update in FROM clause
> 时间: 0s

3、参考:https://dev.mysql.com/doc/refman/5.7/en/update.html ,在 MySQL 中,您不能修改在 SELECT 部分中使用的同一个表。可以将表连接到自身,这将导致 MySQL 将表视为两个不同的事物,从而允许进行破坏性更改。如图2

图2

UPDATE `xx_themes` AS t
INNER JOIN `xx_themes` AS f ON t.id=248 and f.id=237
SET t.is_original = f.is_original
永夜