我确定这个问题毫无意义,对不起,最好的解释方法就是视觉上。
我的表是: 类别,评论,成员和评论。
我有一个查询,从3条最新评论中选择信息
SELECT `reviewID` , `reviewTitle` , `reviewContent` , `reviewDate` , `gameRating` , `reviewImage` , `firstName` , `lastName` , `categoryName`
FROM member
INNER JOIN review
USING ( memberID )
INNER JOIN category
USING ( categoryID )
ORDER BY `reviewDate` DESC
LIMIT 3
每个评论都会分配一个评论ID,评论也会分配一个评论ID来确定评论的评论。我还要计算每次审核的评论数量。评论表包括:
commentID reviewID memberID commentDate commentContent
我试过
SELECT `reviewID`, `reviewTitle`, `reviewContent`, `reviewDate`, `gameRating`, `reviewImage`, `firstName`, `lastName`, `categoryName`, count(commentID) AS comments
FROM member INNER JOIN
review
USING (memberID) INNER JOIN
category
USING (categoryID) INNER JOIN
comment USING (reviewID)
ORDER BY `reviewDate` DESC
LIMIT 3
但它只给出result 这是正确的,因为该评论有2条评论,但其他2条评论有0条评论所以我认为它应该只返回null而不是一起显示其他评论?任何帮助将不胜感激。
答案 0 :(得分:0)
您可能只需要left join
s:
SELECT member.*, count(commentID) AS comments
FROM member LEFT JOIN
review
USING (memberID) LEFT JOIN
category
USING (categoryID) LEFT JOIN
comment
USING (reviewID)
GROUP BY memberId
ORDER BY `reviewDate` DESC
LIMIT 3
您需要GROUP BY
,但您还应删除所有非成员列。