连接多个表并使用 MySQL 计数
Joining multiple Tables and using Count with MySQL
所以我把自己搞得一团糟。
基本上,我有一个社交网络应用程序,我有 4 个不同的表格需要合并到一个视图中。
- Table 1 - 用户 posts
- Table 2 - 用户
- Table 3 - 赞
- Table 4 - 评论
然后我需要 return 一个包含用户详细信息的 post 列表,然后分别为每个 post 添加点赞数和评论数列。
如果 post 没有任何喜欢或评论,那么理想情况下我们应该显示零。
下面的查询将所有内容连接起来,但是 return 包含多行所有内容,因为它 return 为每个评论或类似内容添加 1 行。
谁能帮我把这些结合起来?
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
如有任何帮助,我们将不胜感激!
Table列如下;
app_likes
- like_id
- post_id
- user_id
- liked_date
app_comments
- comment_id
- 评论_user_id
- post_id
- comment_body
- comment_date
app_posts
- post_id
- user_id
- post_content
- post_date
- post_public
app_user
- user_id
- user_first
- user_last
- user_avatar
- user_banned
当前 returned 的示例如下(为方便起见,已删减)
您会看到 post_id 重复了多次。
我想要的 return 只是 post_id 一次,并且在新列中包含 'likes' 和 'comments' 的计数(我不知道如何做到这一点)。
西蒙
您可能缺少 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
请注意,MySQL 让您可以像这样草率地使用 GROUP BY,但是很多其他数据库 would require 您可以将“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
尝试在查询末尾添加分组依据,如下所示
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
所以我把自己搞得一团糟。
基本上,我有一个社交网络应用程序,我有 4 个不同的表格需要合并到一个视图中。
- Table 1 - 用户 posts
- Table 2 - 用户
- Table 3 - 赞
- Table 4 - 评论
然后我需要 return 一个包含用户详细信息的 post 列表,然后分别为每个 post 添加点赞数和评论数列。
如果 post 没有任何喜欢或评论,那么理想情况下我们应该显示零。
下面的查询将所有内容连接起来,但是 return 包含多行所有内容,因为它 return 为每个评论或类似内容添加 1 行。
谁能帮我把这些结合起来?
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
如有任何帮助,我们将不胜感激!
Table列如下;
app_likes
- like_id
- post_id
- user_id
- liked_date
app_comments
- comment_id
- 评论_user_id
- post_id
- comment_body
- comment_date
app_posts
- post_id
- user_id
- post_content
- post_date
- post_public
app_user
- user_id
- user_first
- user_last
- user_avatar
- user_banned
当前 returned 的示例如下(为方便起见,已删减)
您会看到 post_id 重复了多次。
我想要的 return 只是 post_id 一次,并且在新列中包含 'likes' 和 'comments' 的计数(我不知道如何做到这一点)。
西蒙
您可能缺少 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
请注意,MySQL 让您可以像这样草率地使用 GROUP BY,但是很多其他数据库 would require 您可以将“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
尝试在查询末尾添加分组依据,如下所示
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