0

在问答系统中,我试图获得对问答的评论以及该评论的标题。

到目前为止,我已经完成并获得了评论的内容,但现在的问题是,如果评论是关于答案的,那么它没有得到问题的标题。那么我该如何编写查询,以便它可以从问题和答案中获得所有评论,并获得问题的标题。

到目前为止,我已经做到了。

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'))
4

1 回答 1

0

尝试这个

 SELECT * FROM qa_posts p
    LEFT JOIN qa_users u ON c.userid = u.userid
     WHERE p.type = 'C'
       AND p.flagcount = 0
       ORDER BY p.postid DESC
       LIMIT 5
于 2013-08-02T18:51:42.707 回答