MySQL按柱1 /列2排序

时间:2015-03-17 09:13:04

标签: mysql

SELECT `player`,`kills`,`deaths` FROM `stats` ORDER BY `kills` DESC LIMIT 0 , 10

如何制作ORDER BY 来自(KILLS / DEATHS)的最大价值

感谢

---------------------------
| player | kills | deaths |
---------------------------
| user1  | 20    | 2      | 
---------------------------
| user2  | 10    | 2      |
---------------------------
| user3  | 30    | 2      | 
---------------------------

KDR =杀死死亡率=杀死/死亡

所以像这样的结果顺序

1. user3 = 15

2. user1 = 10

3. user2 = 5

2 个答案:

答案 0 :(得分:1)

以下是您问题的解决方案:

SELECT `player`,`kills`,`deaths`,`assist`
FROM `stats`
ORDER BY (
    CASE WHEN `kills` > `deaths` THEN `kills` ELSE `deaths`
)
DESC LIMIT 0 , 10

诀窍是使用CASE语句选择两列中较大的一列'kills''deaths'来排序结果。

答案 1 :(得分:0)

嗯,我想你也想把k_d_ratio显示为一个值,所以我建议这个解决方案。

  SELECT `player`,`kills`,`deaths`,`assist`, ISNULL 
     (kill /(NULLIF(deaths,0), 
      99999999999) 
    as k_d_ratio FROM stats 
   ORDER BY `k_d_ratio` DESC LIMIT 0 , 10