gpt4 book ai didi

MYSQL 更新与 WHERE SELECT 子查询错误

转载 作者:IT老高 更新时间:2023-10-28 23:44:25 25 4
gpt4 key购买 nike

我在获取选择子查询以处理 UPDATE 时遇到问题。我正在尝试以下方法:

UPDATE foo
SET bar=bar-1
WHERE baz=
(
SELECT baz
FROM foo
WHERE fooID='1'
)

其中foo 是主键fooID 的表名。 barbaz 是 INT 类型。执行此操作时出现以下错误:

Error: A query failed. You can't specify target table 'foo' for update 
in FROM clause

最佳答案

从此web article

The reason for this error is that MySQL doesn’t allow updates to a table when you are also using that same table in an inner select as your update criteria. The article goes on to provide a solution, which is to use a temporary table.

使用这个例子,你的更新应该是这样的:

update foo
set bar = bar - 1
where baz in
(
select baz from
(
select baz
from foo
where fooID = '1'
) as arbitraryTableName
)

关于MYSQL 更新与 WHERE SELECT 子查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6944165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com