查询每个班级排名第一的学生信息

查询每个班级排名第一的学生信息

create table cs
(
 name varchar(20),
 class varchar(20),
 ranking int
)

insert into cs values('张三','一班','2')
insert into cs values('王五','一班','5')
insert into cs values('李林','一班','8')
insert into cs values('杨洋1','二班','1')
insert into cs values('李四','二班','3')
insert into cs values('白平','三班','4')
insert into cs values('思杨','三班','6')
insert into cs values('张浩','四班','7')

select c1.* from cs c1 left outer join 
(select c2.CLASS,min(c2.RANKING) as RANKING from cs c2 group by c2.CLASS) t on 
c1.CLASS = t.CLASS where t.RANKING = c1.RANKING 

你可能感兴趣的:(查询)