时间:2021-07-01 10:21:17 帮助过:5人阅读
方法1
左连接
SELECT m1.* FROM messages m1 LEFT JOIN messages m2 ON (m1.name = m2.name AND m1.id < m2.id) WHERE m2.id IS NULL;
子查询
select * from messages where id in (select max(id) from messages group by Name)
mysql8 窗口函数
WITH ranked_messages AS ( SELECT m.*, ROW_NUMBER() OVER (PARTITION BY name ORDER BY id DESC) AS rn FROM messages AS m ) SELECT * FROM ranked_messages WHERE rn = 1;
233
Mysql 技巧 —— 分组后每条记录取最新
标签:max join left join mes null ESS sele nbsp group