基于 SQL 中的行 ID 合并列的问题
An Issue with merging columns based on the row ID in SQL
我需要根据列名称 ID
的行合并 comment
列 我有一个 SQL 查询,以便合并对评论的回复 posts 正在考虑回复 posts 的计数值,即 comment_counts
。我需要在新闻提要 post 的单个线程中显示嵌套评论。
下面是我的 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
对于单个新闻提要 post,多个评论显示在带有嵌套评论的单独提要中。
虽然嵌套评论重复显示。但是有单独的新闻提要线程
预期结果:
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
我只需要显示嵌套评论(不重复),只有一个新闻 post 作为一个线程
例如,对于带有消息 "How are you doing" 的单个新闻提要,正如我用 "Fine" 和 "Not Good"
评论的那样
对于 post "How are you doing" 它重复两次。因为我已经用文本 "Fine" 和 "Not Good"
评论了两次
我应该如何防止嵌套评论作为线程在单个新闻提要中重复两次。
通过使用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 查询以便合并获得的结果中的行以获得 desired/expected 结果?
尝试使用 row_number() 和条件聚合
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
我需要根据列名称 ID
的行合并 comment
列 我有一个 SQL 查询,以便合并对评论的回复 posts 正在考虑回复 posts 的计数值,即 comment_counts
。我需要在新闻提要 post 的单个线程中显示嵌套评论。
下面是我的 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
对于单个新闻提要 post,多个评论显示在带有嵌套评论的单独提要中。 虽然嵌套评论重复显示。但是有单独的新闻提要线程
预期结果:
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
我只需要显示嵌套评论(不重复),只有一个新闻 post 作为一个线程
例如,对于带有消息 "How are you doing" 的单个新闻提要,正如我用 "Fine" 和 "Not Good"
评论的那样
对于 post "How are you doing" 它重复两次。因为我已经用文本 "Fine" 和 "Not Good"
评论了两次
我应该如何防止嵌套评论作为线程在单个新闻提要中重复两次。
通过使用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 查询以便合并获得的结果中的行以获得 desired/expected 结果?
尝试使用 row_number() 和条件聚合
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