我試圖返回所有的消息,表格中的user_id(1)按照created_at desc排序,但根據created_at是否更新,按sender_id或recipient_id進行分組。MySQL按2列分組
messages
sender_id | recipient_id | text | created_at
1 | 2 | hey | 2017-03-26 04:00:00
1 | 2 | tees | 2017-03-26 00:00:00
2 | 1 | rrr | 2017-03-27 00:00:00
3 | 1 | edd | 2017-03-27 00:00:00
1 | 3 | cc3 | 2017-02-27 00:00:00
理想的情況下它會返回
2 | 1 | rrr | 2017-03-27 00:00:00
1 | 3 | cc3 | 2017-02-27 00:00:00
查詢我有多遠 -
select *
from messages
where (
sender_id = 1
or recipient_id = 1
)
group by least(sender_id, recipient_id)
order by created_at desc
,但現在看來,這是由一羣由之前做的順序。
任何幫助將是值得讚賞的。
order by least(...) – Ibu