select * from 表名 where 学号 in (select min(学号) from 表名 group by 班级)
--oR select A.* from 表 A where 学号=(select min(学号) from 表 where 班级=A.班级)
--测试环境 declare @t table(学号 int identity(1,1),班级 int,姓名 varchar(10)) insert into @t select '11','AA' union all select '11','BB' union all select '22','CC' union all select '22','DD'--查询 select * from @t A where not exists (select 1 from @t where 班级=A.班级 and 学号<A.学号)select A.* from @t A where 学号=(select min(学号) from @t where 班级=A.班级)--结果 /* 学号 班级 姓名 ----------- ----------- ---------- 1 11 AA 3 22 CC(所影响的行数为 2 行)学号 班级 姓名 ----------- ----------- ---------- 1 11 AA 3 22 CC(所影响的行数为 2 行) */
select 表.* from 表 a inner join (select 班级,min(学号) as 学号 from 表 order by 学号 group by 班级) b on a.学号 = b.学号
from 表名
where 学号 in (select min(学号) from 表名 group by 班级)
select A.* from 表 A
where 学号=(select min(学号) from 表 where 班级=A.班级)
declare @t table(学号 int identity(1,1),班级 int,姓名 varchar(10))
insert into @t select '11','AA'
union all select '11','BB'
union all select '22','CC'
union all select '22','DD'--查询
select * from @t A
where not exists (select 1 from @t where 班级=A.班级 and 学号<A.学号)select A.* from @t A
where 学号=(select min(学号) from @t where 班级=A.班级)--结果
/*
学号 班级 姓名
----------- ----------- ----------
1 11 AA
3 22 CC(所影响的行数为 2 行)学号 班级 姓名
----------- ----------- ----------
1 11 AA
3 22 CC(所影响的行数为 2 行)
*/
(select 班级,min(学号) as 学号 from 表 order by 学号 group by 班级) b
on a.学号 = b.学号