declare @t table(ID int, ConID int, Flag int, State int) insert @t select 1 , 1 , 0 , 0 insert @t select 2 , 2 , 0 , 1 insert @t select 3 , 2 , 2 , 0 --法一 select * from @t t where not exists (select 1 from @t where conid=t.conid and id>t.id) --法二 select * from @t t where id=(select top 1 id from @t where conid=t.conid order by id desc)ID ConID Flag State ----------- ----------- ----------- ----------- 1 1 0 0 3 2 2 0(2 行受影响)
insert @t select 1 , 1 , 0 , 0
insert @t select 2 , 2 , 0 , 1
insert @t select 3 , 2 , 2 , 0 --法一
select * from @t t where not exists (select 1 from @t where conid=t.conid and id>t.id)
--法二
select * from @t t where id=(select top 1 id from @t where conid=t.conid order by id desc)ID ConID Flag State
----------- ----------- ----------- -----------
1 1 0 0
3 2 2 0(2 行受影响)