[英]SQL - #1093 - Table '' is specified twice, both as a target for 'UPDATE' and as a separate source for data
I have this code: 我有以下代码:
UPDATE WORLD_TEST
SET PROGRAMMER = (SELECT (RECURSE_HOUR/360)
FROM WORLD_TEST_PROGRAMMER, WORLD_TEST
WHERE LVL = LVL_PROGRAMMER)
WHERE (SELECT MAX_RECURSE
FROM WORLD_TEST_PROGRAMMER, WORLD_TEST
WHERE LVL = LVL_PROGRAMMER)
>=
(PROGRAMMER+(SELECT (RECURSE_HOUR/360)
FROM WORLD_TEST_PROGRAMMER, WORLD_TEST
WHERE LVL = LVL_PROGRAMMER));
With this error: 出现此错误:
1093 - Table 'WORLD_TEST' is specified twice, both as a target for 'UPDATE' and as a separate source for data
1093-表'WORLD_TEST'被指定两次,既作为'UPDATE'的目标,又作为数据的单独源
EDIT (clarification from comments): PROGRAMMER
and LVL_PROGRAMMER
is from WORLD_TEST
table, and RECURSE_HOUR
, LVL
, MAX_RECURSE
is from WORLD_TEST_PROGRAMMER
table. 编辑(从注释中澄清):
PROGRAMMER
和LVL_PROGRAMMER
来自WORLD_TEST
表,而RECURSE_HOUR
, LVL
和MAX_RECURSE
来自WORLD_TEST_PROGRAMMER
表。
Error message is quite clear, that you cannot use the same table in the UPDATE
clause as well as the source of a subquery. 错误消息非常清楚,您不能在
UPDATE
子句中使用同一表以及子查询的源。 There are other ways to achieve that, like using Derived Tables, etc. In this particular case, you simply need to JOIN
between the two tables. 还有其他方法可以实现此目的,例如使用派生表等。在这种特定情况下,您只需要在两个表之间进行
JOIN
。
Also, please don't use Old comma based Implicit joins and switch to Modern Explicit Join
based syntax 另外,请不要使用基于旧逗号的隐式连接,而不要使用基于现代显式
Join
的语法
In case of multi-table queries, it is recommended to use Aliasing for code readability and avoiding ambiguous behavior. 对于多表查询,建议使用别名来提高代码的可读性并避免歧义行为。
Based on your further clarification in the comments , try the following: 根据您在评论中的进一步说明,尝试以下操作:
UPDATE WORLD_TEST AS wt
JOIN WORLD_TEST_PROGRAMMER AS wtp
ON wt.LVL_PROGRAMMER = wtp.LVL
SET wt.PROGRAMMER = wtp.RECURSE_HOUR
WHERE wtp.MAX_RECURSE >= (wt.PROGRAMMER + (wtp.RECURSE_HOUR/360))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.