执行MYSQL 报 You can't specify target table 'seller' for update in FROM clause 错。
具体错误信息。
1 queries executed, 0 success, 1 errors, 0 warnings
查询:update seller set source = 15 where id in ( select id from seller WHERE `status` = 'Wait' and assignee_id = '0' and last_process...
错误代码: 1093
You can't specify target table 'seller' for update in FROM clause
执行耗时 : 0 sec
传送时间 : 0 sec
总耗时 : 0.018 sec
执行的SQl
UPDATE
seller
SET
source = 15
WHERE id IN
(SELECT
id
FROM
seller
WHERE `status` = 'Wait'
) ;
报错如题,意思大致是:在一条 sql 语句中不能先查出来部分内容,再同时又对当前表作修改。
解决方法:给查询加别名,用中间表来实现不是对同一表作操作。
修改后的SQL
UPDATE
seller
SET
source = 15
WHERE id IN
(SELECT
a.id
FROM
(
SELECT
id
FROM
seller
WHERE `status` = 'Wait' ) a
) ;
MySQL中不能先select出同一表中的某些值,再update这个表(在同一语句中)。将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,mssql和Oracle不会出现此问题。