Mysql不允许update表和子查询中表为同一张表
1.问题描述:
UPDATE refund SET state = 3
WHERE id =
(SELECT id FROM refund WHERE order_number = 'testNumber' ORDER BY create_time DESC LIMIT 0,1)
这条sql报错:
You can’t specify target table 'refund ’ for update in FROM clause
译为:不能在FROM子句中为update指定目标表“refund”
2.解决方法:嵌套子查询,给里面的表取别名
UPDATE refund SET state = 3
WHERE id =
(SELECT id FROM (SELECT id FROM refund WHERE order_number = 'testNumber' ORDER BY create_time DESC LIMIT 0,1)temp)
执行成功!
UPDATE refund SET state = 3
WHERE id =
(SELECT id FROM (SELECT id FROM refund WHERE order_number = ‘testNumber’ ORDER BY create_time DESC LIMIT 0,1)temp)
Affected rows: 0
时间: 0.025s