MYSQL QUERY 三重连接 + 三重计数
MYSQL QUERY triple join + triple count
我需要执行查询,但无法使其正常工作...
任何帮助将不胜感激!
执行于 MYSQL WORKBENCH
我有一个由 4 个表组成的数据库 :
user
Posts
likes
comments
username
id
id
id
id
title
userId (foreign key from user.id)
content
email
content
postId (foreign key from posts.id)
date
password
date
userId (foreign key from user.id)
image
imageUrl
postId (foreign key from posts.id)
bio
userId (foreign key from user.id)
createDate
我想要一个查询,该查询将从 USER 获取特定数据加上特定用户的喜欢、帖子和评论的总数,如果该用户不存在喜欢/帖子/评论,则总结果为 0。
我基本上需要我的用户的统计信息。
我现在的查询给了我 username / permission / image / bio 和 createDate 的好信息,但是 return 我 0 for all计数.
SELECT u.username, u.permission, u.image, u.bio, u.createDate,
COALESCE(P.totalPosts, 0) AS totalPosts,
COALESCE(C.totalComms, 0) AS totalComms,
COALESCE(L.totalLikes, 0) AS totalLikes
FROM user AS u
LEFT JOIN(
SELECT userId, COUNT(*) AS totalPosts
FROM posts
) AS P ON P.userId = u.id
LEFT JOIN(
SELECT userId, COUNT(*) AS totalComms
FROM comments
) AS C ON C.userId = u.id
LEFT JOIN(
SELECT userId, COUNT(*) AS totalLikes
FROM likes
) AS L ON L.userId = u.id
WHERE u.username = 'Alfred';
谢谢你的时间 <3 !
您缺少的是:
GROUP BY userId
在你所有的子查询中:
SELECT u.username, u.permission, u.image, u.bio, u.createDate,
COALESCE(P.totalPosts, 0) AS totalPosts,
COALESCE(C.totalComms, 0) AS totalComms,
COALESCE(L.totalLikes, 0) AS totalLikes
FROM user AS u
LEFT JOIN(
SELECT userId, COUNT(*) AS totalPosts
FROM posts
GROUP BY userId
) AS P ON P.userId = u.id
LEFT JOIN(
SELECT userId, COUNT(*) AS totalComms
FROM comments
GROUP BY userId
) AS C ON C.userId = u.id
LEFT JOIN(
SELECT userId, COUNT(*) AS totalLikes
FROM likes
GROUP BY userId
) AS L ON L.userId = u.id
WHERE u.username = 'Alfred';
但是,所有这些子查询都会对所有用户进行聚合,而不仅仅是对您想要结果的特定用户进行聚合,这对性能不利。
如果使用相关子查询会更好:
SELECT u.username, u.permission, u.image, u.bio, u.createDate,
(SELECT COUNT(*) FROM posts p WHERE p.userId = u.id) AS totalPosts,
(SELECT COUNT(*) FROM comments c WHERE c.userId = u.id) AS totalComms,
(SELECT COUNT(*) FROM likes l WHERE l.userId = u.id) AS totalLikes
FROM user AS u
WHERE u.username = 'Alfred';
我需要执行查询,但无法使其正常工作...
任何帮助将不胜感激!
执行于 MYSQL WORKBENCH
我有一个由 4 个表组成的数据库 :
user | Posts | likes | comments |
---|---|---|---|
username | id | id | id |
id | title | userId (foreign key from user.id) | content |
content | postId (foreign key from posts.id) | date | |
password | date | userId (foreign key from user.id) | |
image | imageUrl | postId (foreign key from posts.id) | |
bio | userId (foreign key from user.id) | ||
createDate |
我想要一个查询,该查询将从 USER 获取特定数据加上特定用户的喜欢、帖子和评论的总数,如果该用户不存在喜欢/帖子/评论,则总结果为 0。 我基本上需要我的用户的统计信息。
我现在的查询给了我 username / permission / image / bio 和 createDate 的好信息,但是 return 我 0 for all计数.
SELECT u.username, u.permission, u.image, u.bio, u.createDate,
COALESCE(P.totalPosts, 0) AS totalPosts,
COALESCE(C.totalComms, 0) AS totalComms,
COALESCE(L.totalLikes, 0) AS totalLikes
FROM user AS u
LEFT JOIN(
SELECT userId, COUNT(*) AS totalPosts
FROM posts
) AS P ON P.userId = u.id
LEFT JOIN(
SELECT userId, COUNT(*) AS totalComms
FROM comments
) AS C ON C.userId = u.id
LEFT JOIN(
SELECT userId, COUNT(*) AS totalLikes
FROM likes
) AS L ON L.userId = u.id
WHERE u.username = 'Alfred';
谢谢你的时间 <3 !
您缺少的是:
GROUP BY userId
在你所有的子查询中:
SELECT u.username, u.permission, u.image, u.bio, u.createDate,
COALESCE(P.totalPosts, 0) AS totalPosts,
COALESCE(C.totalComms, 0) AS totalComms,
COALESCE(L.totalLikes, 0) AS totalLikes
FROM user AS u
LEFT JOIN(
SELECT userId, COUNT(*) AS totalPosts
FROM posts
GROUP BY userId
) AS P ON P.userId = u.id
LEFT JOIN(
SELECT userId, COUNT(*) AS totalComms
FROM comments
GROUP BY userId
) AS C ON C.userId = u.id
LEFT JOIN(
SELECT userId, COUNT(*) AS totalLikes
FROM likes
GROUP BY userId
) AS L ON L.userId = u.id
WHERE u.username = 'Alfred';
但是,所有这些子查询都会对所有用户进行聚合,而不仅仅是对您想要结果的特定用户进行聚合,这对性能不利。
如果使用相关子查询会更好:
SELECT u.username, u.permission, u.image, u.bio, u.createDate,
(SELECT COUNT(*) FROM posts p WHERE p.userId = u.id) AS totalPosts,
(SELECT COUNT(*) FROM comments c WHERE c.userId = u.id) AS totalComms,
(SELECT COUNT(*) FROM likes l WHERE l.userId = u.id) AS totalLikes
FROM user AS u
WHERE u.username = 'Alfred';