[英]Joining multiple Tables and using Count with MySQL
So I've got myself in a massive confused mess. 所以我陷入了混乱的混乱之中。
Basically, I have a little social network app and I have 4 different tables I need to combine into one view. 基本上,我有一个小的社交网络应用程序,我需要将4个不同的表合并为一个视图。
I then need to return a list of posts, with the user details and then add columns for the number of likes and number of comments for each post respectively. 然后,我需要返回包含用户详细信息的帖子列表,然后分别为每个帖子的点赞次数和评论数添加列。
If a post doesn't have any likes or comments, then ideally we should show a Zero. 如果帖子没有任何喜欢或评论,则理想情况下,我们应显示零。
The query below joins everything up, but returns multiple rows of everything, as it is returning 1 row for each comment or like as well. 下面的查询将所有内容合并在一起,但返回所有内容的多行,因为它为每个注释或类似内容返回1行。
Anyone able to help me combine these together? 有谁能帮助我将这些结合在一起?
SELECT *
FROM app_posts AS p
LEFT JOIN app_comments AS c ON c.post_id = p.post_id
LEFT JOIN app_user AS u ON u.user_id = p.user_id
LEFT JOIN app_likes AS l ON l.post_id = p.post_id
WHERE u.user_banned = 0
AND p.post_public = 1
ORDER BY p.post_date DESC
Any help would be greatly appreciated! 任何帮助将不胜感激!
Table columns are as follows; 表列如下;
app_likes app_likes
app_comments app_comments
app_posts app_posts
app_user app_user
An example of what is returned currently is as follows (chopped down for easiness) 当前返回的示例如下(为方便起见而被切掉)
You'll see the post_id is repeated multiple times. 您会看到post_id被重复多次。
What I want it to return is the post_id just once, and with the count of 'likes' and 'comments' in new columns (I don't know how to do this). 我想要它返回的只是post_id一次,并且在新列中包含“喜欢”和“评论”的计数(我不知道该怎么做)。
Simon 西蒙
You might be missing GROUP BY... 您可能缺少GROUP BY ...
SELECT p.*,u.*,count(distinct c.comment_id),count(distinct l.like_id)
FROM app_posts AS p
LEFT JOIN app_comments AS c ON c.post_id = p.post_id
LEFT JOIN app_user AS u ON u.user_id = p.user_id
LEFT JOIN app_likes AS l ON l.post_id = p.post_id
WHERE u.user_banned = 0
AND p.post_public = 1
GROUP BY p.post_id
ORDER BY p.post_date DESC
Note that MySQL lets you be sloppy with GROUP BY like this, but a lot of other databases would require you to break out the " p.*
" into explicit MAX(p.post_id),MAX(p.post_content),
etc. 请注意,MySQL这样使您对GROUP BY MAX(p.post_id),MAX(p.post_content),
草率,但是许多其他数据库将要求您将“ p.*
”分解为显式的MAX(p.post_id),MAX(p.post_content),
等。
SELECT p.post_id, COUNT(c.post_id) as num_comments, COUNT(l.like_id) as num_likes
FROM app_posts AS p
LEFT JOIN app_comments AS c ON c.post_id = p.post_id
LEFT JOIN app_user AS u ON u.user_id = p.user_id
LEFT JOIN app_likes AS l ON l.post_id = p.post_id
WHERE u.user_banned = 0
AND p.post_public = 1
GROUP BY p.post_id
ORDER BY p.post_date DESC
Try to add Group by at the end of your query, like this 尝试在查询末尾添加Group by
SELECT *
FROM app_posts AS p
LEFT JOIN app_comments AS c ON c.post_id = p.post_id
LEFT JOIN app_user AS u ON u.user_id = p.user_id
LEFT JOIN app_likes AS l ON l.post_id = p.post_id
WHERE u.user_banned = 0
AND p.post_public = 1
GROUP BY p.post_id
ORDER BY p.post_date DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.