gpt4 book ai didi

mysql - 使用联接更新多个表

转载 作者:行者123 更新时间:2023-11-29 12:15:31 26 4
gpt4 key购买 nike

我有以下查询,效果很好:

$query = "UPDATE
mpt
JOIN
compounds
ON
compounds.mpt = mpt.id
SET
mpt.supervisor_comments = " . $this->db->escape($comments['mpt']) . "
WHERE
compounds.id = " . $id;

并更新链接到化合物表中具有特定 ID 的行的 mpt 表。

然后我尝试扩展查询以一次更新两个字段,但是,此时我被告知:

Unknown column 'mpt.id' in 'on clause'

UPDATE mpt, bpt JOIN compounds ON compounds.mpt = mpt.id AND compounds.bpt = bpt.id SET mpt.supervisor_comments = 'abcd' WHERE compounds.id = 5

尽管在前面的示例中它运行得很好...有什么建议或建议吗?

$query = "UPDATE
mpt, bpt
JOIN
compounds
ON
compounds.mpt = mpt.id AND compounds.bpt = bpt.id
SET
mpt.supervisor_comments = " . $this->db->escape($comments['mpt']) . "
WHERE
compounds.id = " . $id;

最佳答案

JOIN 的优先级运算符比表列表中的逗号更高,因此在加入 bpt 时与 compoundsmpt尚未加入。明显的修复计数是像这样放置括号:

UPDATE (mpt, bpt) JOIN compounds ON ...

关于mysql - 使用联接更新多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29871468/

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