我的查询是
UPDATE xxx_tickets
LEFT JOIN xxx_ticket_comments
ON xxx_tickets.ID = xxx_ticket_comments.ticket_id
SET xxx_tickets.status = 'open'
WHERE xxx_ticket_comments.private = '1'
AND xxx_tickets.org_code = '9'
AND xxx_tickets.ID = '2'
ORDER BY xxx_ticket_comments.ID DESC LIMIT 1
我遇到的问题是第二个表xxx_ticket_comments
有多个与ticket_id
相关的记录。我需要的是加入表格,以便我获得xxx_ticket_comments
ORDER BY在我的陈述中不起作用,但我离开了它,以便您可以了解我想要完成的任务。
简而言之,我需要根据xxx_tickets
xxx_ticket_comments
希望它有意义。
答案 0 :(得分:1)
您可以将条件移至WHERE
子句。
UPDATE xxx_tickets t LEFT JOIN
xxx_ticket_comments tc
ON t.ID = tc.ticket_id AND
tc.private = 1
SET t.status = 'open'
WHERE t.org_code = 9 AND t.ID = 2 AND
tc.ID = (SELECT MAX(tc2.ID)
FROM xxx_ticket_comments tc2
WHERE tc2.ticket_id = tc.ticket_id AND
tc2.private = 1
);
不幸的是,ORDER BY
不支持LIMIT
和JOIN
。