【发布时间】:2017-03-09 04:29:23
【问题描述】:
我正在尝试获取与我下载的每个帖子相关的最新 1 或 2 个 cmets,有点像 instagram 所做的,因为他们为每个帖子显示最新的 3 cmets,到目前为止,我正在获取帖子和点赞数。
现在我需要做的就是弄清楚如何获得最新的 cmets,不太确定如何接近它,这就是为什么我希望有更多专业知识的人可以帮助我!
这是我当前的查询:
(SELECT
P.uuid,
P.caption,
P.imageHeight,
P.path,
P.date,
U.id,
U.fullname,
U.coverImage,
U.bio,
U.username,
U.profileImage,
coalesce(Activity.LikeCNT,0),
Activity.CurrentUserLiked
FROM USERS AS U
INNER JOIN Posts AS P
ON P.id = U.id
LEFT JOIN (SELECT COUNT(DISTINCT Activity.uuidPost) LikeCNT, Activity.uuidPost, Activity.id, sum(CASE WHEN Activity.id = $id then 1 else 0 end) as CurrentUserLiked
FROM Activity Activity
WHERE type = 'like'
GROUP BY Activity.uuidPost) Activity
ON Activity.uuidPost = P.uuid
AND Activity.id = U.id
WHERE U.id = $id)
UNION
(SELECT
P.uuid,
P.caption,
P.imageHeight,
P.path,
P.date,
U.id,
U.fullname,
U.coverImage,
U.bio,
U.username,
U.profileImage,
coalesce(Activity.LikeCNT,0),
Activity.CurrentUserLiked
FROM Activity AS A
INNER JOIN USERS AS U
ON A.IdOtherUser=U.id
INNER JOIN Posts AS P
ON P.id = U.id
LEFT JOIN (SELECT COUNT(DISTINCT Activity.uuidPost) LikeCNT, Activity.uuidPost, Activity.id, sum(CASE WHEN Activity.id = $id then 1 else 0 end) as CurrentUserLiked
FROM Activity Activity
WHERE type = 'like'
GROUP BY Activity.uuidPost) Activity
ON Activity.uuidPost = P.uuid
AND Activity.id = U.id
WHERE A.id = $id)
ORDER BY date DESC
LIMIT 0, 5
基本上cmets和likes存储在同一张表中。
所以表是Activity,那么我有一个列comment存储评论文本,然后“类型”等于“评论”。
可能解释得不是很好,但我愿意尝试提供尽可能多的细节!
如果有人能提供帮助,非常感谢!
更新
在https://stackoverflow.com/users/1016435/xqbert 给出的这个查询中,我目前遇到了这个错误:
用于操作“=”的排序规则 (utf8_general_ci,IMPLICIT) 和 (utf8_unicode_ci,IMPLICIT) 的非法混合
SELECT Posts.id,
Posts.uuid,
Posts.caption,
Posts.path,
Posts.date,
USERS.id,
USERS.username,
USERS.fullname,
USERS.profileImage,
coalesce(A.LikeCNT,0),
com.comment
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 comment, UUIDPOST, @row_num := IF(@prev_value=UUIDPOST,@row_num+1,1) as row_number,@prev_value := UUIDPOST
FROM Activity
CROSS JOIN (SELECT @row_num := 1) x
CROSS JOIN (SELECT @prev_value := '') y
WHERE type = 'comment'
ORDER BY UUIDPOST, date DESC) Com
ON Com.UUIIDPOSt = Posts.UUID
AND row_number <= 2
ORDER BY date DESC
LIMIT 0, 5
最新编辑
表结构:
帖子
----------------------------------------------------------
| id | int(11) | | not null |
| uuid | varchar(100) | utf8_unicode_ci | not null |
| imageLink | varchar(500) | utf8_unicode_ci | not null |
| date | timestamp | | not null |
----------------------------------------------------------
用户
-------------------------------------------------------------
| id | int(11) | | not null |
| username | varchar(100) | utf8_unicode_ci | not null |
| profileImage | varchar(500) | utf8_unicode_ci | not null |
| date | timestamp | | not null |
-------------------------------------------------------------
活动
----------------------------------------------------------
| id | int(11) | | not null |
| uuid | varchar(100) | utf8_unicode_ci | not null |
| uuidPost | varchar(100) | utf8_unicode_ci | not null |
| type | varchar(50) | utf8_unicode_ci | not null |
| commentText | varchar(500) | utf8_unicode_ci | not null |
| date | timestamp | | not null |
----------------------------------------------------------
这些是一些示例,在这种情况下,“活动”表中的“类型”将始终等于“评论”。
总结一切并渴望结果:
当我查询用户的帖子时,我希望能够进入“活动”表并为他的每个帖子获取最新 2 cmets。也许不会有 cmets,所以很明显它会返回 0,也许那个帖子可能有 100 个 cmets。但我只想获取最新/最近的 2 cmets。
一个例子可以看看 Instagram 是如何做到的。对于每个帖子,显示最近的 cmets 1、2 或 3....
希望这会有所帮助!
【问题讨论】:
-
如果您在问题中包含表定义,将会得到很好的解释。
-
活动中的日期列的名称是什么?或者你如何确定最近的 1-2 cmets 是什么?)每个活动是否有唯一的序列,所以如果有,那么最高的 ID 是什么列名?或者是否有一个日期表示最近的活动,如果有,它的名称是什么?
-
请包含示例表数据和预期的查询输出。
-
请为每个相关表格提供
SHOW CREATE TABLE。
标签: php mysql sql greatest-n-per-group