0

我正在尝试更新 MySql 中的 2 个不同的表,同时我将它们都内部连接在一起。

这是我最后一次尝试返回以下错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM ticketing_system_issues AS i INNER JOIN ( SELECT d.notes, d.issue' at line 8

UPDATE i, dis
SET i.category_id = 5,
i.priority = 1,
i.issue_title = "This is a test",
dis.notes = "This is a notes test",
i.modified_on = NOW(),
i.modified_by = 2
FROM ticketing_system_issues AS i
INNER JOIN (
SELECT d.notes, d.issue_id FROM ticketing_system_discussions AS d
INNER JOIN (
SELECT issue_id, MIN(created_on) AS min_time FROM ticketing_system_discussions
GROUP BY issue_id
) AS sub ON sub.issue_id = d.issue_id AND d.created_on = sub.min_time

) AS dis ON i.issue_id = dis.issue_id
WHERE i.issue_id = 58

如需更多测试以下查询以查找我喜欢更新的字段

SELECT i.category_id, i.priority, i.issue_title, dis.notes, i.modified_on, i.modified_by
FROM ticketing_system_issues AS i
INNER JOIN (
SELECT d.notes, d.issue_id FROM ticketing_system_discussions AS d
INNER JOIN (
SELECT issue_id, MIN(created_on) AS min_time FROM ticketing_system_discussions
GROUP BY issue_id
) AS sub ON sub.issue_id = d.issue_id AND d.created_on = sub.min_time

) AS dis ON i.issue_id = dis.issue_id
WHERE i.issue_id = 58
4

1 回答 1

3

你想要的一般语法是:

UPDATE table1 AS t1
JOIN table2 AS t2 ON <join condition>
SET t1.col1 = val1, t1.col2 = val2, t2.col3 = val3
WHERE <filter condition>

你的SET条款放错地方了。

不可能UPDATE在您的问题中执行特定操作,因为您正在尝试分配给子查询,而不是表。您必须将其分成您分配的表和您加入的子查询:

UPDATE ticketing_system_issues AS i
INNER JOIN ticketing_system_discussions AS dis ON i.issue_id = dis.issue_id
INNER JOIN (SELECT issue_id, MIN(created_on) AS min_time
            FROM ticketing_system_discussions
            GROUP BY issue_id) AS sub
      ON sub.issue_id = dis.issue_id AND dis.created_on = sub.min_time
SET i.category_id = 5,
    i.priority = 1,
    i.issue_title = "This is a test",
    dis.notes = "This is a notes test",
    i.modified_on = NOW(),
    i.modified_by = 2
WHERE i.issue_id = 58
于 2013-05-23T19:48:43.813 回答