gpt4 book ai didi

mysql - 尝试用其他表中的数字更新列(子查询返回多于 1 行)

转载 作者:行者123 更新时间:2023-11-30 23:12:12 25 4
gpt4 key购买 nike

我有 2 个表:quest_mail_loot_template(表 1)和 quest_template(表 2)。他们都有“条目”列,这是他们的主要 ID 键。他们互不依赖。

Table2 的列 RewMailTemplateId 可以为空,但我只需要 RewMailTemplateId<>0 .我需要找到两个表中的条目相同的行,当它发生时,根据 Table2 的 RewMailTemplateId 列的编号更改 Table1 中的每个条目。

我试过用这个:

update quest_mail_loot_template Table1 set entry=(
select RewMailTemplateId
from quest_template where RewMailTemplateId<>0)
where (
select Table2.RewMailTemplateId
from quest_template TBL2
where TBL2.entry = TBL1.entry
);

似乎 sql 正在尝试用所有找到的数字更新 1 个字段,但我需要为他自己的 RewMailTemplateId 更新每个条目,这是不同的数字。

我至少应该使用哪些运算符有什么建议吗?

最佳答案

UPDATE table1 a
JOIN table2 b
ON a.entry = b.entry
AND b.RewMailTemplateId<>0 AND b.RewMailTemplateId!=a.entry
SET a.entry = b.RewMailTemplateId

或者

UPDATE table1 a
JOIN table2 b
ON a.entry = b.entry
SET a.entry = b.RewMailTemplateId WHERE b.RewMailTemplateId<>0 AND b.RewMailTemplateId!=a.entry

关于mysql - 尝试用其他表中的数字更新列(子查询返回多于 1 行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19364674/

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