Я ограничил значение комментария до 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
Вы можете попробовать приведенный выше запрос с помощью
Я хочу избавиться от значений NULL, занимающих комментарий1,2 ... и чтобы значение комментария оставалось на первом месте, в противном случае NULL должен быть заполнен в случае, если это не такимеют значение.