在问题和答案系统中我想获得评论的问题和答案和该评论的标题。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