【发布时间】:2021-10-27 12:06:57
【问题描述】:
我在这里有 2 张桌子很重要。一个表称为 POSTS,包含帖子和回复(回复与帖子的处理方式完全相同,但它们在 POSTS.reply 中包含引用帖子的 POSTS.id)。
我的第二个表称为 social 并且包含 POST.id 作为外键,旁边是视图和点赞 + 点赞和查看它的用户。
我使用这个 SELECT 来获取我所有的帖子数据,包括标题、内容...喜欢和视图:
SELECT posts.id, posts.username, posts.time, cat.cat_name,
posts.title, posts.content, posts.reply,
posts.user_file, posts.audio,
social.views, social.likes
FROM posts
LEFT JOIN user on posts.user_id = user.id
LEFT JOIN cat ON posts.cat_id = cat.id
LEFT JOIN social ON posts.id = social.post_id
WHERE social.likes IN (SELECT social.likes FROM social
WHERE social.id IN (SELECT MAX(social.id)
FROM social GROUP BY post_id))
GROUP BY social.post_id
HAVING posts.reply = 0
我不知道如何让一行包含响应特定帖子的回复数(1 行应该包含我要在页面上显示的所有数据)。
我不知道如何让一行包含响应特定帖子的回复数(1 行应该包含我要在页面上显示的所有数据)。
通常我会得到这样的 cmets,但我不知道如何合并它们:)
SELECT * FROM posts WHERE posts.reply != 0
结构如下:
从我希望收到的查询中:
"username" "time" "cat_name" "title" "content" "reply" "user_file" "audio" "views" "likes" "id" "username"
"xxx_user_xxx" "YYYY-MM-DD" "topic" "Title." "NSJNASNJSAN?" "reply: 1/not reply: 0" "/user_uploads/xxx.yyy" "audio or video" "number of views" "number oflikes"
现在我想获得一个额外的列 REPLIES(这样做 SELECT * FROM posts WHERE posts.reply != 0)并获取每个帖子的回复数。
【问题讨论】:
-
请提供包含少量样本数据和预期输出的表结构。
-
也许这会清除一点。
-
如果两个表很重要,为什么示例代码引用了五个或六个表?