如何通过id DESC选择唯一的"名称"订单列表,限制5
用户表结构:
id name
1 John
2 John
3 Andrew
4 John
5 Sarah
6 dan
7 Sarah
8 Simon
9 Sarah
10 John
导出结果
name:
John
Sarah
Simon
dan
Andrew
谢谢,
我认为这是最简单的方法:
select name from table_name
group by name
order by max(id) desc
limit 5
不需要联接。
以下是
select t1.name from table_name t1
left join table_name t2 on t1.name = t2.name and t1.id < t2.id
where t2.id is null
order by t1.id desc limit 5 ;
SELECT name from (
SELECT DISTINCT name
FROM nameTable
ORDER BY id DESC
)