2015-02-23 64 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