2015-02-23 63 views
3

我有多个列如如何计算前3结果

RacerID | Round1 | Round2 | Round3 | Round4 | Total 
    2   100  100  96  99  395            
    5   99  97  100  96  392 

如何可以查询出从列(ROUND1,round2,round3,round4),使得它应该显示顶层3的结果

racerid | Top3Rounds 
    2   299     
    5   296 

非常感谢:)

+0

你想sql查询或php代码?你有什么尝试吗?的 – Jigar 2015-02-23 08:23:38

+0

可能重复[多列的SQL MAX?(http://stackoverflow.com/questions/71022/sql-max-of-multiple-columns) – Tom 2015-02-23 08:23:46

+1

你也考虑正常化你的数据结构。 – 2015-02-23 08:31:31

回答

6

可能是这样的:

SELECT RacerID , round1 + round2 + round3 + round4 - LEAST(round1, round2, round3, round4) AS Top3Rounds 
FROM tablename 

小提琴示例here

+2

随意添加小提琴。 – 2015-02-23 08:29:28

+1

@超级公牛,谢谢,这是更好的 – RIKI 2015-02-23 08:31:46

+2

伟大的工作! @OTARIKI – 2015-02-23 08:34:04