我有4张桌子:
posts [post_id post_title post_body post_date post_by post_accepted]
users [user_id user_name user_pw]
comments [comment_id comment_user comment_co comment_post comment_date]
categories [categorie_id categorie_name]
每个表都包含一个数据,所有数据都属于表格帖子:
categories<-posts
posts<-users
posts<-comments<-users
我想获取id为post_id的帖子并获得评论,并发布帖子并获取类别名称和ID。
我试过,但是我收到了帖子但不是所有评论,或者如果帖子没有评论,它就不会出现。
这是我的SQL查询:
SELECT COUNT(comment_id),comments.*,categories.*,users.*,posts.*
FROM posts
JOIN categories on (posts.post_id = categories.categorie_id)
JOIN users on (posts.post_by = users.user_id)
LEFT JOIN comments on (posts.post_id = comments.comment_post)
WHERE posts.post_id='34'
AND posts.post_accepted = '1' ;
答案 0 :(得分:0)
尝试将GROUP BY users.id添加到您的查询