使用pivot表获取结果时的一个问题

lokaqttq  于 2021-06-19  发布在  Mysql
关注(0)|答案(0)|浏览(280)

我将评论的值限制为7。在下面的查询中,注解的结果值将移向以 NULL 价值观。

set @row_number=0;

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,
min(case when seq=3 then comment end) as comment3,
min(case when seq=3 then comment_timestamp end) as commenttime3,
min(case when seq=4 then comment end) as comment4,
min(case when seq=4 then comment_timestamp end) as commenttime4,
min(case when seq=5 then comment end) as comment5,
min(case when seq=5 then comment_timestamp end) as commenttime5,
min(case when seq=6 then comment end) as comment6,
min(case when seq=6 then comment_timestamp end) as commenttime6,
min(case when seq=7 then comment end) as comment7,
min(case when seq=7 then comment_timestamp end) as commenttime7
from
(
select *,
        (@row_number := @row_number +1) 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

实际结果:

id  userid  content     timestamp   cmt_count   comment1    commenttime1    comment2      commenttime2  comment3    commenttime3    comment4    commenttime4    comment5    commenttime5    comment6                        commenttime6 comment7   commenttime7
1   14      How are     14:07:00    2           NULL        NULL            NULL          NULL          NULL        NULL            NULL        NULL            NULL        NULL            NULL                            NULL         Look Good  20:00:00
            you?
2   14      How are     13:06:42    3           NULL        NULL            NULL          NULL          Not Good    15:05:10        Fine        15:00:15        Fine        15:00:15        Not Bad After All your efforts! 08:13:10     NULL           NULL
            you doing?
3   1       This is     13:12:31    2           new cmt     14:00:15        manju comment 13:17:31      NULL        NULL            NULL        NULL            NULL        NULL            NULL                            NULL         NULL           NULL
            manju

预期结果

id  userid  content             timestamp   comment_count   comment1        commenttime1    comment2    commenttime2 comment3                           commenttime3
1   14      How are you?        14:07:00    2               Look Good       20:00:00        So I'm!     20:10:00     NULL                               NULL
2   14      How are you doing?  13:06:42    3               Fine            15:00:15        Not Good    15:05:10     Not Bad After All your efforts!    08:13:10
3   1       This is manju       13:12:31    2               manju comment   13:17:31        new cmt     14:00:15     NULL                               NULL

您可以尝试以上查询

sql小提琴

我想去掉注解1,2中的空值。。。因此,注解的值应保持在第一位,否则如果没有值,则应填充null。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题