现有表 test 字段如下
calssID userid chengji name sex
1 1 50 张3 男
1 2 51 李四 男
1 3 52 王五 男
2 4 56 张31 男
2 5 57 李四1 男
2 6 58 王五1 男
3 7 60 张32 男
3 8 62 李四2 男
3 9 63 王五2 男
现在要通过classID分组 也就是分成n组
查询出来每一组的成绩最高的学生name,sex,chengji,userid
谁能帮我写一下sql语句 立马给分!!!
calssID userid chengji name sex
1 1 50 张3 男
1 2 51 李四 男
1 3 52 王五 男
2 4 56 张31 男
2 5 57 李四1 男
2 6 58 王五1 男
3 7 60 张32 男
3 8 62 李四2 男
3 9 63 王五2 男
现在要通过classID分组 也就是分成n组
查询出来每一组的成绩最高的学生name,sex,chengji,userid
谁能帮我写一下sql语句 立马给分!!!
create table test(
calssID int,
userid int,
chengji int,
name varchar(10),
sex varchar(2)
)
insert into test
select 1,1,50,'张3','男' union all
select 1,2,51,'李四','男' union all
select 1,3,52,'王五','男' union all
select 2,4,56,'张31','男' union all
select 2,5,57,'李四','男' union all
select 2,6,58,'王五1','男' union all
select 3,7,60,'张32','男' union all
select 3,8,62,'李四2','男' union all
select 3,9,63,'王五2','男'
goselect t.* from test t where chengji=(select max(chengji) from test where calssID=t.calssID) order by t.calssIDdrop table test
group by classID
having chengji = max(chengji) ;
group by classID
having chengji = max(chengji) ;
正解
select * from test where chengji in (select max(chengji) from test group by calssID)
我就是需要补习一下sql了
呵呵!
生命不息 学习不止
谢谢各位!!!
如果谁有更好的方法 希望继续讨论!
select * from test t where t.chengji in
(SELECT max(chengji) FROM test group by calssID)
[/align]