我正在尝试获取属于我在当前查询中关注的用户的帖子。
目前我正在收到:当前用户的帖子 - 比如每个帖子的数量&和评论。
我需要得到这样的结果:当前用户的帖子 - 我关注帖子的人 - 比如每个帖子的数量&评价。
最终结果与大多数社交网络主页一样。他们会收到您的帖子以及您关注的人/您朋友的帖子。
这是我目前的查询:
SELECT
Posts.id
, Posts.uuid
, max(case when rcom.row_number = 1 then rcom.comment end) Comment_one
, max(case when rcom.row_number = 2 then rcom.comment end) Comment_two
, Posts.caption
, Posts.path
, Posts.`date`
, USERS.id
, USERS.username
, USERS.fullname
, USERS.profileImage
, COALESCE(A.LikeCNT, 0) num_likes
FROM Posts
INNER JOIN USERS ON Posts.id = 145
AND USERS.id = 145
LEFT JOIN (
SELECT
COUNT(A.uuidPost) LikeCNT
, A.UUIDPost
FROM Activity A
WHERE type = 'like'
GROUP BY
A.UUIDPOST
) A ON A.UUIDPost = Posts.uuid
LEFT JOIN (
SELECT
@row_num := IF(@prev_value=UUIDPOST,@row_num+1,1) as row_number
, comment
, uuidPost
, `date`
, @prev_value := UUIDPOST
FROM Activity
CROSS JOIN ( SELECT @row_num := 1, @prev_value := '' collate utf8_unicode_ci ) xy
WHERE type = 'comment'
ORDER BY
uuidPost
, `date` DESC
) rcom ON rcom.uuidPost = Posts.UUID
AND rcom.row_number <= 2
GROUP BY
Posts.id
, Posts.uuid
, Posts.caption
, Posts.path
, Posts.`date`
, USERS.id
, USERS.username
, USERS.fullname
, USERS.profileImage
, COALESCE(A.LikeCNT, 0)
ORDER BY date DESC
;
我的粉丝是如何存储的?
我将关注者存储在“活动”表中,如下所示: id (currentUser) - idOtherUser (我关注的人) - 类型(等于“跟随”)。
就是这样。这是一个带有一些示例数据的sql fiddle。
如有任何问题,请告知我们。
答案 0 :(得分:1)
这可能就是你要找的东西。
我假设每个用户都必须自己点击关注按钮。此外,这是从用户145的角度编写的,您必须更改WHERE
- 子句。在您的示例中,用户145跟随用户123没有关系,我添加它以测试查询。
我从选择列表中删除了Users.id
,因为它与Posts.id
相同。
类似计数的一些示例数据本来不错,不知道如何填写表格。
SELECT
*
FROM
(
SELECT
Posts.id,
Posts.uuid,
max(
CASE
WHEN rcom.row_number = 1 THEN
rcom.commentText
END
) Comment_one,
max(
CASE
WHEN rcom.row_number = 1 THEN
rcom.commentUserName
END
) Comment_one_user,
max(
CASE
WHEN rcom.row_number = 2 THEN
rcom.commentText
END
) Comment_two,
max(
CASE
WHEN rcom.row_number = 2 THEN
rcom.commentUserName
END
) Comment_two_user,
Posts.`date`,
USERS.username,
USERS.profileImage,
COALESCE (A.LikeCNT, 0) num_likes
FROM
Posts
INNER JOIN USERS ON Posts.id = USERS.id
LEFT JOIN (
SELECT
COUNT(A.uuidPost) LikeCNT,
A.UUIDPost
FROM
Activity A
WHERE
type = 'like'
GROUP BY
A.UUIDPOST
) A ON A.UUIDPost = Posts.uuid
LEFT JOIN (
SELECT
@row_num :=
IF (
@prev_value = UUIDPOST ,@row_num + 1,
1
) AS row_number,
commentText,
uuidPost,
Activity.`date`,
@prev_value := UUIDPOST,
USERS.id AS commentUserId,
USERS.username AS commentUserName
FROM
Activity
CROSS JOIN (
SELECT
@row_num := 1,
@prev_value := '' COLLATE utf8_unicode_ci
) xy
INNER JOIN USERS ON USERS.id = Activity.id
WHERE
type = 'comment'
ORDER BY
uuidPost,
`date` DESC
) rcom ON rcom.uuidPost = Posts.UUID AND rcom.row_number <= 2
WHERE Posts.id = 145
GROUP BY
Posts.id,
Posts.uuid,
Posts.`date`,
USERS.username,
USERS.profileImage,
COALESCE (A.LikeCNT, 0)
UNION
SELECT
Posts.id,
Posts.uuid,
max(
CASE
WHEN rcom.row_number = 1 THEN
rcom.commentText
END
) Comment_one,
max(
CASE
WHEN rcom.row_number = 1 THEN
rcom.commentUserName
END
) Comment_one_user,
max(
CASE
WHEN rcom.row_number = 2 THEN
rcom.commentText
END
) Comment_two,
max(
CASE
WHEN rcom.row_number = 2 THEN
rcom.commentUserName
END
) Comment_two_user,
Posts.`date`,
USERS.username,
USERS.profileImage,
COALESCE (A.LikeCNT, 0) num_likes
FROM
Posts
INNER JOIN Activity ON Activity.type = 'Follow' AND Posts.id = Activity.IdOtherUser
INNER JOIN USERS ON Activity.IdOtherUser = USERS.id
LEFT JOIN (
SELECT
COUNT(A.uuidPost) LikeCNT,
A.UUIDPost
FROM
Activity A
WHERE
type = 'like'
GROUP BY
A.UUIDPOST
) A ON A.UUIDPost = Posts.uuid
LEFT JOIN (
SELECT
@row_num :=
IF (
@prev_value = UUIDPOST ,@row_num + 1,
1
) AS row_number,
commentText,
uuidPost,
Activity.`date`,
@prev_value := UUIDPOST,
USERS.id AS commentUserId,
USERS.username AS commentUserName
FROM
Activity
CROSS JOIN (
SELECT
@row_num := 1,
@prev_value := '' COLLATE utf8_unicode_ci
) xy
INNER JOIN USERS ON USERS.id = Activity.id
WHERE
type = 'comment'
ORDER BY
uuidPost,
`date` DESC
) rcom ON rcom.uuidPost = Posts.UUID AND rcom.row_number <= 2
WHERE Activity.id = 145
GROUP BY
Posts.id,
Posts.uuid,
Posts.`date`,
USERS.username,
USERS.profileImage,
COALESCE (A.LikeCNT, 0)
) AS posts
ORDER BY
posts.`date` DESC;
修改强>
在选择帖子后整理缩进并加入评论+ likecount之后:
SELECT
posts.id,
posts.uuid,
max(CASE WHEN rcom.row_number = 1 THEN rcom.commentText END) AS Comment_one,
max(CASE WHEN rcom.row_number = 1 THEN rcom.commentUserName END) Comment_one_user,
max(CASE WHEN rcom.row_number = 2 THEN rcom.commentText END) Comment_two,
max(CASE WHEN rcom.row_number = 2 THEN rcom.commentUserName END) Comment_two_user,
posts.`date`,
posts.username,
posts.profileImage,
COALESCE(A.LikeCNT) AS likes
FROM
(
SELECT
Posts.id,
Posts.uuid,
Posts.`date`,
USERS.username,
USERS.profileImage
FROM
Posts
INNER JOIN USERS
ON Posts.id = USERS.id
WHERE
Posts.id = 145
GROUP BY
Posts.id,
Posts.uuid,
Posts.`date`,
USERS.username,
USERS.profileImage
UNION
SELECT
Posts.id,
Posts.uuid,
Posts.`date`,
USERS.username,
USERS.profileImage
FROM
Posts
INNER JOIN Activity
ON Activity.type = 'Follow' AND Posts.id = Activity.IdOtherUser
INNER JOIN USERS
ON Activity.IdOtherUser = USERS.id
WHERE
Activity.id = 145
GROUP BY
Posts.id,
Posts.uuid,
Posts.`date`,
USERS.username,
USERS.profileImage
) AS posts
LEFT JOIN (
SELECT
COUNT(A.uuidPost) LikeCNT,
A.UUIDPost
FROM
Activity A
WHERE
type = 'like'
GROUP BY
A.UUIDPOST
) AS A ON A.UUIDPost = posts.uuid
LEFT JOIN (
SELECT
commentText,
uuidPost,
`date`,
commentUserId,
commentUserName,
@row_num := IF (@prev_value = UUIDPOST, @row_num + 1, 1) AS row_number,
@prev_value := UUIDPOST
FROM
(
SELECT
commentText,
uuidPost,
Activity.`date`,
USERS.id AS commentUserId,
USERS.username AS commentUserName
FROM
Activity
INNER JOIN USERS
ON USERS.id = Activity.id
WHERE
type = 'comment'
ORDER BY
uuidPost,
`date` DESC
) AS comments
CROSS JOIN (
SELECT
@row_num := 1,
@prev_value := '' COLLATE utf8_unicode_ci
) AS xy
) rcom
ON rcom.uuidPost = posts.UUID AND rcom.row_number <= 2
GROUP BY
posts.uuid
ORDER BY
posts.`date` DESC