SQL中基于行ID合并列的问题
我需要根据列名为SQL中基于行ID合并列的问题,sql,pivot,mariadb,group-concat,nested-queries,Sql,Pivot,Mariadb,Group Concat,Nested Queries,我需要根据列名为ID的行合并comment列,以便将对已评论帖子的回复合并到已回复帖子的计数值,即comment\u counts。我需要在新闻提要的单个线程中显示嵌套注释。下面是我的SQL查询 SELECT DISTINCT ft.ID as ID, ft.userid, ft.content, ft.timestamp, ft.comments as comment_counts, ftc.comment, ftc.timestamp as comment_timestamp, uq.use
ID
的行合并comment
列,以便将对已评论帖子的回复合并到已回复帖子的计数值,即comment\u counts
。我需要在新闻提要的单个线程中显示嵌套注释。下面是我的SQL查询
SELECT DISTINCT ft.ID as ID, ft.userid, ft.content, ft.timestamp, ft.comments as comment_counts, ftc.comment, ftc.timestamp as comment_timestamp, uq.username, uq.avatar
FROM users uq, feed_item ft
LEFT JOIN feed_item_comment ftc ON ftc.postid = ft.ID
LEFT JOIN user_friends uf ON uf.friendid = ftc.userid
LEFT JOIN users u ON u.ID = uf.friendid WHERE uq.ID = ft.userid AND ft.userid
IN
(SELECT u.ID FROM users u WHERE u.ID
IN (SELECT uf.friendid FROM user_friends uf WHERE uf.status = '2' AND uf.userid = '".$this->user->info->ID."')
OR
u.ID
IN (SELECT uf.userid FROM user_friends uf WHERE uf.status = '2' AND uf.friendid = '".$this->user->info->ID."')
OR
u.ID = '".$this->user->info->ID."'
) ORDER BY ft.ID DESC, ftc.timestamp DESC
实际结果: 这是从上述查询中获得的结果
ID userid content timestamp comment_counts comment comment_timestamp username avatar
___________________________________________________________________________________________________________________________________________
3 1 This is manju 13:12:31 2 manju comment 13:17:31 manju 1698862132.png
3 1 This is manju 13:12:31 2 new cmt 14:00:15 manju 1698862132.png
2 14 How are you doing? 13:06:42 2 Fine 15:00:15 Nishanth default.png
2 14 How are you doing? 13:06:42 2 Not Good 15:05:10 Nishanth default.png
1 14 How are you? 14:07:00 2 Look Good 20:00:00 Nishanth default.png
1 14 How are you? 14:07:00 2 So I'm! 20:10:00 Nishanth default.png
对于包含多条注释的单个新闻提要帖子,在包含嵌套注释的单独提要中显示。
尽管嵌套注释重复显示。但是有单独的新闻提要线程
预期结果:
ID userid content timestamp comment_counts comment1 comment1_timestamp comment2 comment2_timestamp username avatar
_______________________________________________________________________________________________________________________________________________________
3 1 This is manju 13:12:31 2 manju comment 13:17:31 new cmt 14:00:15 manju 1698862132.png
2 14 How are you doing? 13:06:42 2 Fine 15:00:15 Not Good 15:05:10 Nishanth default.png
1 14 How are you? 14:07:00 2 Look Good 20:00:00 So I'm! 20:10:00 Nishanth default.png
我只需要以一条新闻帖子作为一个线程显示嵌套评论(不重复)例如,对于带有消息“您好”的单个新闻提要,正如我用“好”和“不好”评论的那样
“你好”一文重复了两遍。因为我已经用文本“好”和“不好”评论了两次
我需要如何防止嵌套注释在单个新闻提要中作为线程重复两次 使用
GROUP_CONCAT(ftc.comment)作为回复
ID userid content timestamp comment_counts comment comment_timestamp username avatar replies
__________________________________________________________________________________________________________________________________________________________________________
1 14 How are you? 14:07:00 2 Look Good 20:00:00 Nishanth default.png Look Good,Fine,Not Good,manju comment,new cmt,Look...
使用查询完成数据库: 使用
GROUP\u CONCAT(ftc.comment)
作为回复
ID userid content timestamp comment_counts comment comment_timestamp username avatar replies
__________________________________________________________________________________________________________________________________________________________________________
1 14 How are you? 14:07:00 2 Look Good 20:00:00 Nishanth default.png Look Good,Fine,Not Good,manju comment,new cmt,Look...
我应该如何编写SQL查询,以便从获得的结果中合并行,以获得所需/预期的结果?尝试下面使用row_number()和条件聚合
您可能需要阅读数据透视表。虽然您想要做的不是典型的pivot,但您仍然在尝试将行转换为列,pivot就是这么做的。您的mysql版本@nishanthMariaDB服务器版本是什么:10.1.36@fa06I感谢您的努力。查询运行得非常好。但是回复的动态值和帖子的时间戳,即评论计数>2。既然我已经附加了(7,2,14,'你的努力还不错!','08:13:10',0,0,0),我该如何处理你的问题呢;对于发布的
内容值=你做得怎么样?
从提要项目表中,将注释作为回复值=不好添加到表中。
从提要项目表中,它应该作为动态回复的一部分显示在下一列的后面这篇评论文章。从而自动创建一个新的栏来回复帖子。威尔,你能帮我吗@尼桑斯ॐ, 你能把它加到你的预期输出吗?请稍等minute@Nishanthॐ maria db您的版本支持pivot吗?
select id, userid,content,timestamp,comment_count,
min(case when seq=1 then comment end) as comment1,
min(case when seq=1 then comment_timestamp end) as commenttime1,
min(case when seq=2 then comment end) as comment2,
min(case when seq=2 then comment_timestamp end) as commenttime2
from
(
select *,row_number() over(partition by id,userid order by comment_timestamp) as seq from
(SELECT DISTINCT ft.ID as ID, ft.userid, ft.content, ft.timestamp, ft.comments as comment_count, ftc.comment, ftc.timestamp as comment_timestamp, uq.username, uq.avatar FROM users uq,
feed_item ft
LEFT JOIN feed_item_comment ftc ON ftc.postid = ft.ID
LEFT JOIN user_friends uf ON uf.friendid = ftc.userid
LEFT JOIN users u ON u.ID = uf.friendid WHERE uq.ID = ft.userid AND ft.userid
IN
(SELECT u.ID FROM users u WHERE u.ID
IN (SELECT uf.friendid FROM user_friends uf WHERE uf.status = '2' AND uf.userid = 1)
OR u.ID IN
(SELECT uf.userid FROM user_friends uf WHERE uf.status = '2' AND uf.friendid = 1)
OR
u.ID = 1
)ORDER BY ft.ID DESC, ftc.timestamp DESC)X)Y
group by id, userid,content,timestamp,comment_count