基于 SQL 中的行 ID 合并列的问题
Posted
技术标签:
【中文标题】基于 SQL 中的行 ID 合并列的问题【英文标题】:An Issue with merging columns based on the row ID in SQL 【发布时间】: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 【参考方案1】:在下面尝试使用 row_number() 和条件聚合
DEMO
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
【讨论】:
感谢您的辛勤努力。查询工作正常。但是对于帖子的回复和时间戳的动态值,即comment_count> 2。我应该如何处理您的查询因为我已经附加了(7、2、14,'在你所有的努力之后还不错!',' 08:13:10', 0, 0, 0);到feed_item_comment
表Click Here
对于 feed_item_comment
和 comment
作为回复值 = Not Good
对于已发布的 content
值 = How are you doing?
来自 feed_item
表 它应该显示在下一个列作为对评论帖子的动态回复的一部分。从而自动为帖子的回复创建一个新列。威尔,你能帮我这样做吗?!
@Nishanthॐ,能否将其添加到您的预期输出中
没问题
@Nishanthॐ maria db 你的版本支持支点吗?以上是关于基于 SQL 中的行 ID 合并列的问题的主要内容,如果未能解决你的问题,请参考以下文章
Pandas 将具有多个值的行数据合并到列的 Python 列表中