使用Join更新多个表

时间:2015-04-25 22:32:00

标签: mysql

我有以下查询可以正常工作:

$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;

1 个答案:

答案 0 :(得分:1)

JOIN运算符的优先级高于表格列表中的逗号,因此当bpt加入compounds表格mpt时,尚未加入UPDATE (mpt, bpt) JOIN compounds ON ... 。显而易见的修复计数是将括号括起来:

$(window).scrollTo(0,$("#btnAddContent").offset().top);