MYSQL update with WHERE SELECT subquery error(带有 WHERE SELECT 子查询错误的 MYSQL 更新)
问题描述
我在获取选择子查询以处理 UPDATE
时遇到问题.我正在尝试类似以下内容:
I have an issue with getting select sub-queries to work on an UPDATE
. I'm trying something like the following:
UPDATE foo
SET bar=bar-1
WHERE baz=
(
SELECT baz
FROM foo
WHERE fooID='1'
)
其中foo
是主键为fooID
的表名.bar
和 baz
是 INT 类型.执行此操作时出现以下错误:
Where foo
is the table name with primary key fooID
. bar
and baz
are of type INT. When executing this I get the following error:
Error: A query failed. You can't specify target table 'foo' for update
in FROM clause
推荐答案
从此 网络文章
此错误的原因是当您还在内部选择中使用同一表作为更新条件时,MySQL 不允许对表进行更新.文章接着提供了一个解决方案,那就是使用临时表.
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.
使用这个例子,你的更新应该是这样的:
Using this example, your update should be this:
update foo
set bar = bar - 1
where baz in
(
select baz from
(
select baz
from foo
where fooID = '1'
) as arbitraryTableName
)
这篇关于带有 WHERE SELECT 子查询错误的 MYSQL 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 WHERE SELECT 子查询错误的 MYSQL 更新


基础教程推荐
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01