在SQL中查找跨列的最大值

问题描述 投票:-1回答:1

我有一个4列SID,物理,化学,数学的表。无论主题如何,我都需要获得SID和最高分,任何人都可以帮助我。

对于Eg

SID Physics Chemistry Maths
1      25      30       85
2      45      28       91
3      97      40       76

产量

SID  Max_Marks 
3     97
sql
1个回答
1
投票

大多数数据库都支持least()greatest()。所以,你可以这样做:

select sid, greatest(Physics, Chemistry, Maths) as max_marks
from t
order by max_marks desc
limit 1;

这种语法更适合MySQL。但是,您几乎可以在任何数据库中执行类似操作。

© www.soinside.com 2019 - 2024. All rights reserved.