用max函数。
语法:
select max(字段1),max(字段2),max(字段3),max(字段4) from 表名
结果就是字段1,字段2,字段3,字段4分别的最大值,如果要查询其他字段的最大值,按照上述方式添加即可。
函数简介:
MAX() 函数
MAX 函数返回一列中的最大值。NULL 值不包括在计算中。
SQL MAX() 语法
SELECT MAX(column_name) FROM table_name
注释:MAX 也可用于文本列,以获得按字母顺序排列的最高或最低值。
select max(high) as high ,studentid
from
(
select chinese as high,studentid from score
union all
select maths as high,studentid from score
union all
select english as high,studentid from score
) as highscore
group by studentid
这个效率有问题,但是能解决你的问题,再增加一门的话再union下就行了。
随便写的,没调,有错自己改下。
我在研究你的问题..正在测试..你等等再采纳..
经过测试,标准答案:
select case when chinese>maths and chinese>english then chinese
when maths>chinese and maths>english then maths
when english>chinese and english>maths then english
end
from test_score
这样更详细:
select case when chinese>maths and chinese>english then convert(nvarchar(5),chinese)+N'语文'
when maths>chinese and maths>english then convert(nvarchar(5),maths)+N'数学'
when english>chinese and english>maths then convert(nvarchar(5),english)+N'英语'
end
from test_score
select
studentid,
chinese,
maths,
english,
(select max(t.a) from (select chinese as a union select maths union select english) t) as [最高分],
(select min(t.a) from (select chinese as a union select maths union select english) t) as [最低分]
from score