使用数据透视表获取结果时出现问题

时间:2018-10-22 13:35:13

标签: mysql pivot pivot-table

我将评论的值限制为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 Fiddle

我想摆脱注释1,2 ...占据的NULL值,以便注释的值应保持在第一位,否则应填充NULL,以防它没有该值。

0 个答案:

没有答案