select * from 表名 AS t where 分數 = (select max(分數) from 表名 where 姓名=T.姓名)
create table 表名(STUDENTID int, 姓名 varchar(100), 分數 int) insert into 表名 select 1,'A',92 union all select 2,'A',94 union all select 3,'B',98 union all select 4,'B',99 union all select 5,'C',98select * from 表名 AS t where 分數 = (select max(分數) from 表名 where 姓名=T.姓名) order by STUDENTIDdrop table 表名
我得不在到這樣的結果! 2 A 94 4 B 99 5 C 98不行吧!
select a.* from tb a, (select 姓名, max(分數) as 分数 group by 姓名) b where a.姓名=b.姓名 and a.分数=b.分数
2 A 94 4 B 99 5 C 98-------------------------你要的不是这样的数据?
if object_id('pubs..tb') is not null drop table tb gocreate table tb(STUDENTID int,姓名 varchar(10),分数 int) insert into tb(STUDENTID,姓名,分数) values(1, 'A', 92) insert into tb(STUDENTID,姓名,分数) values(2, 'A', 94) insert into tb(STUDENTID,姓名,分数) values(3, 'B', 98) insert into tb(STUDENTID,姓名,分数) values(4, 'B', 99) insert into tb(STUDENTID,姓名,分数) values(5, 'C', 98) go select a.* from tb a, (select 姓名, max(分数) as 分数 from tb group by 姓名) b where a.姓名=b.姓名 and a.分数=b.分数 order by a.姓名drop table tb /* STUDENTID 姓名 分数 ----------- ---------- ----------- 2 A 94 4 B 99 5 C 98(所影响的行数为 3 行)*/
declare @tab table (id int, 姓名 varchar(100), 分数 int) insert into @tab select 1,'A',92 union all select 2,'A',94 union all select 3,'B',98 union all select 4,'B',99 union all select 5,'C',98Select * From @tab t Where 分数 In (Select max(分数) From @tab b Where b.姓名 = t.姓名)(所影响的行数为 5 行)id 姓名 分数 ----------- ---------------------------------------------------------------------------------------------------- ----------- 5 C 98 4 B 99 2 A 94
create table 表名(STUDENTID int, 姓名 varchar(100), 分數 int) insert into 表名 select 1,'A',92 union all select 2,'A',94 union all select 3,'B',98 union all select 4,'B',99 union all select 5,'C',98select * from 表名 AS t where 分數 = (select max(分數) from 表名 where 姓名=T.姓名) order by STUDENTIDdrop table 表名 /* STUDENTID 姓名 分數 ----------- ----------------- 2 A 94 4 B 99 5 C 98 */
SELECT * FROM student WHERE 分数=(SELECT TOP MAX(T.分数) FROM student T WHERE T.姓名 = 姓名)
SELECT * FROM student WHERE 分数=(SELECT TOP 1 MAX(T.分数) FROM student T WHERE T.姓名 = 姓名)上面少了一个 1 ,可能会有同名同分数的情况,返回多条时会出错,所以用TOP 1
from 表名 AS t
where 分數 = (select max(分數) from 表名 where 姓名=T.姓名)
insert into 表名
select 1,'A',92 union all
select 2,'A',94 union all
select 3,'B',98 union all
select 4,'B',99 union all
select 5,'C',98select *
from 表名 AS t
where 分數 = (select max(分數) from 表名 where 姓名=T.姓名)
order by STUDENTIDdrop table 表名
2 A 94
4 B 99
5 C 98不行吧!
(select 姓名, max(分數) as 分数 group by 姓名) b
where a.姓名=b.姓名 and a.分数=b.分数
4 B 99
5 C 98-------------------------你要的不是这样的数据?
drop table tb
gocreate table tb(STUDENTID int,姓名 varchar(10),分数 int)
insert into tb(STUDENTID,姓名,分数) values(1, 'A', 92)
insert into tb(STUDENTID,姓名,分数) values(2, 'A', 94)
insert into tb(STUDENTID,姓名,分数) values(3, 'B', 98)
insert into tb(STUDENTID,姓名,分数) values(4, 'B', 99)
insert into tb(STUDENTID,姓名,分数) values(5, 'C', 98)
go
select a.* from tb a,
(select 姓名, max(分数) as 分数 from tb group by 姓名) b
where a.姓名=b.姓名 and a.分数=b.分数
order by a.姓名drop table tb
/*
STUDENTID 姓名 分数
----------- ---------- -----------
2 A 94
4 B 99
5 C 98(所影响的行数为 3 行)*/
insert into @tab
select 1,'A',92 union all
select 2,'A',94 union all
select 3,'B',98 union all
select 4,'B',99 union all
select 5,'C',98Select * From @tab t Where 分数 In (Select max(分数) From @tab b Where b.姓名 = t.姓名)(所影响的行数为 5 行)id 姓名 分数
----------- ---------------------------------------------------------------------------------------------------- -----------
5 C 98
4 B 99
2 A 94
insert into 表名
select 1,'A',92 union all
select 2,'A',94 union all
select 3,'B',98 union all
select 4,'B',99 union all
select 5,'C',98select *
from 表名 AS t
where 分數 = (select max(分數) from 表名 where 姓名=T.姓名)
order by STUDENTIDdrop table 表名
/*
STUDENTID 姓名 分數
----------- -----------------
2 A 94
4 B 99
5 C 98
*/