这个MySQL查询抛出错误:
Error Code: 1093. You can't specify target table 'myTable' for update in FROM clause
我已尝试过所有以前给出的答案,但我的问题仍未解决,请帮忙。
update myTable set total = total+1
where (x_id, y_id) in
(
select x_id, y_id
from xTable join myTable using(x_id)
where (x_code='NA-204' and y_id in (select y_id from yTable where z_id=5))
or (x_code='PS-204' and y_id in (select y_id from yTable where z_id=1))
);
答案 0 :(得分:0)
这是一个简单的技巧,但它可能很慢:
update myTable set total = total+1
where (x_id, y_id) in
(
SELECT * FROM
(
select x_id, y_id
from xTable join myTable using(x_id)
where (x_code='NA-204' and y_id in (select y_id from yTable where z_id=5))
or (x_code='PS-204' and y_id in (select y_id from yTable where z_id=1))
) s
)
或者您可以使用JOIN:
update
myTable INNER JOIN (
select x_id, y_id
from xTable join myTable using(x_id)
where (x_code='NA-204' and y_id in (select y_id from yTable where z_id=5))
or (x_code='PS-204' and y_id in (select y_id from yTable where z_id=1))
) s
ON myTable.x_id=s.x_id
AND myTable.y_id=s.y_id
set
myTable.total = myTable.total+1