----方法1:
select * from @t as a where not exists(select 1 from @t where ID1 = a.ID1 and ID2 < a.ID2)
这句里面的子查询select 1 from @t where ID1 = a.ID1 and ID2 < a.ID2中
为什么写成select 1 而不写成select * 呢?这个有什么区别
declare @t table(ID1 int,ID2 int)
insert @t
select 15, 17 union all
select 15, 18 union all
select 16, 19
----方法1:
select * from @t as a where not exists(select 1 from @t where ID1 = a.ID1 and ID2 < a.ID2)
----方法2:
select * from @t as a where ID2 = (select min(ID2) from @t where ID1 = a.ID1)
----方法3:
select a.* from @t as a
INNER JOIN (select ID1, min(ID2) as ID2 from @t group by ID1) as b
on a.ID1 = b.ID1 and a.ID2 = b.ID2