【发布时间】:2019-03-19 11:27:22
【问题描述】:
我需要基于列名称为ID 的行合并comment 列我有一个SQL 查询,以便合并对评论帖子的回复,考虑到回复帖子的计数值,即comment_counts。我需要在新闻提要帖子的单个线程中显示嵌套的 cmets。
下面是我的 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
对于具有多个 cmets 的单个新闻提要帖子显示在具有嵌套 cmets 的单独提要中。 尽管嵌套的 cmets 反复显示。但有单独的新闻提要线程
预期结果:
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
我只需要显示嵌套的 cmets(不重复),只有一个新闻帖子作为线程
例如,对于带有“你好吗”消息的单个新闻提要,正如我用“很好”和“不好”评论的那样
对于帖子“你好吗”,它重复了两次。因为我用文字“很好”和“不好”评论了两次
我应该如何防止嵌套的 cmets 在单个新闻提要中作为线程重复两次。
通过使用GROUP_CONCAT(ftc.comment) as replies
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 Fiddle DEMO
使用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...
我应该如何编写 SQL 查询,以便从获得的结果中合并行以获得所需/预期的结果?
【问题讨论】:
-
您可能需要阅读数据透视表。尽管您想要做的不是典型的枢轴,但您仍在尝试将行转换为列,这就是枢轴所做的。
-
你的mysql版本是什么@nishanth
-
MariaDB 服务器版本:10.1.36 @fa06
标签: sql pivot mariadb group-concat nested-queries