我想查询哪里 reciever_id 等于 1 并按 sender_id 分组并获取最新记录。
我使用了查询:
SELECT `t`.* FROM(
SELECT * FROM messages
WHERE reciever_id = 1
ORDER BY created_at DESC
) `t`
GROUP BY `sender_id`
ORDER BY `id`
还有:
SELECT message,MAX(created_at)
FROM messages
WHERE reciever_id = 1
GROUP BY sender_id
ORDER BY created_at
图中日期列created_at是最新的,id也是有序的,也是最新的。