ID Name
1 A
2 A
3 B
4 C
5 C
6 D
7 F
8 F
output 1
---------
ID Name
2 A
5 C
8 F
output 2
--------
ID Name
1 A
3 B
4 C
6 D
7 F
我需要sql select语句来获取output1和输出2
我需要获取输出1中的重复记录的最后一个条目以及output2中所有其余条目
答案 0 :(得分:7)
select max(id) id, name
from tbl
group by name
having count(*) > 1
select tbl.*
from tbl
left join (
select max(id) id
from tbl
group by name
having count(*) > 1) x on x.id = tbl.id
where x.id is null