假設後只能變更一次(即你有POST_CHANGE表有某些限制),可以使用下面的查詢:
SELECT p.id, p.id_category as "Original category", IFNULL(pcl.changed_to, p.id_category) as "New Category", pc.id as comment_id, pc.comment_text, pc.date
FROM posts p LEFT JOIN posts_change_log pcl ON p.id = pcl.id_post
JOIN posts_comments pc ON p.id = pc.id_post
WHERE p.id = ?
如果信息可以改變多次,那麼最好返回/展後的歷史和在單獨的查詢註釋,例如:
SELECT p.id, p.id_category as "Original category", pc.changed_to as "New Category"
FROM post p JOIN posts_change_log pcl
ON p.id = pcl.id_post
WHERE p.id = ?
SELECT *
FROM posts_comments
WHERE id_post = ?
更新
ŧ Ø結合兩個輸出,你可以使用UNION ALL
運營商,例如:
SELECT comment_text, date, "comment" as "type
FROM posts_comments
WHERE id_post = ?
UNION
SELECT changed_to, date, "change" as "type"
FROM post_change_log
WHERE id_post = ?
ORDER BY date;
因此,給予一定的樣本數據,正好將你想要的結果數據集的樣子? –
你已經嘗試過什麼來解決這個問題? –
我假設,你想這是一個單一的查詢,對嗎? –