表:
id stuid stuimage
1 1 a1.jpg
2 1 b1.jpg
3 1 c1.jpg
4 1 d1.jpg
5 2 a2.jpg
6 2 b2.jpg
7 3 a3.jpg
8 3 b3.jpg
9 3 c3.jpg
我想查出 每种 stuid 中的 任意一条或第一条 的记录如:
1 1 a1.jpg
5 2 a2.jpg
7 3 a3.jpg
请各位高手帮忙.....
id stuid stuimage
1 1 a1.jpg
2 1 b1.jpg
3 1 c1.jpg
4 1 d1.jpg
5 2 a2.jpg
6 2 b2.jpg
7 3 a3.jpg
8 3 b3.jpg
9 3 c3.jpg
我想查出 每种 stuid 中的 任意一条或第一条 的记录如:
1 1 a1.jpg
5 2 a2.jpg
7 3 a3.jpg
请各位高手帮忙.....
select *
from tb t
where not exists (select 1 from tb where stuid = t.stuid and id < t.id)
select *
from tb t
where id = (select min(id) from tb where stuid = t.stuid)
create table #TB(id int, stuid int, stuimage nvarchar(10))
insert #TB select 1, 1, 'a1.jpg' union all
select 2 ,1, 'b1.jpg' union all
select 3 ,1, 'c1.jpg' union all
select 4 ,1, 'd1.jpg' union all
select 5, 2, 'a2.jpg' union all
select 6, 2, 'b2.jpg' union all
select 7, 3, 'a3.jpg' union all
select 8 ,3, 'b3.jpg' union all
select 9 ,3, 'c3.jpg'select * from #TB as t1 where id in(select min(id) from #TB group by stuid)