在問題和答案系統中我想獲得評論的問題和答案和該評論的標題。MySQL:如何獲得評論內容和帖子標題
到目前爲止,我已經完成了評論的內容,但現在的問題是如果評論的答案是沒有得到問題標題。那麼我怎麼寫查詢,以便它能從問題和答案中得到所有評論,並且會得到問題的標題。
這裏我已經完成了。
SELECT c.postid, p.title, c.type, c.userid, c.content, c.parentid, u.handle, u.email, u.avatarblobid, u.avatarwidth, u.avatarheight FROM qa_posts p
JOIN qa_posts c ON (p.postid = c.parentid)
LEFT JOIN qa_users u ON c.userid = u.userid
WHERE c.type = 'C'
AND p.flagcount = 0
ORDER BY c.postid DESC
LIMIT 5
這裏是我的表像,你可以找到type
列是Q,C和A的問題,發表評論,並分別回答。
非常感謝
編輯:-------------------------------- -----------------------------
我試圖有條件地檢查,但這段代碼也不工作..我希望你專家將指導,以糾正這種代碼
JOIN qa_posts AS parentposts
ON qa_posts.postid=(
CASE
LEFT(perentposts.type, 1)
WHEN
'A'
THEN
parentposts.parentid
ELSE
parentposts.postid
END
)
JOIN qa_posts AS cposts
ON parentposts.postid=cposts.parentid
LEFT JOIN qa_users AS cusers
ON cposts.userid=cusers.userid
JOIN (SELECT postid FROM qa_posts
WHERE type=$
ORDER BY qa_posts.created DESC
LIMIT 5)
y ON cposts.postid=y.postid
WHERE qa_posts.type='Q'
AND ((parentposts.type='Q') OR (parentposts.type='A'))
所以你有樹型結構。你需要找出一個特定葉子的根節點的屬性。這裏的問題在於,您並沒有真正將數據設置爲有效執行此操作的方式。我會推薦這篇文章http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/,它描述瞭如何在MySQL(或真正的任何關係數據庫)中存儲分層結構數據。 –
非常感謝Mike的參考。然而,這是開源腳本之一,而不是我寫的MySQL。我仍在學習。但是,這將是很好的幫助.. – Artist
任何人都請幫助我.. – Artist