按间隔对列2进行排序,并对第1列进行排序

时间:2018-03-18 13:45:04

标签: mysql range between

我需要一个SQL语句,它首先按1000 Hours步的间隔进行排序,然后按DESC顺序对Points进行排序。 我无法弄清楚如何在此示例中使用CaseBetween / case

目前的结果 SELECT * FROM CurrencyUser ORDER BY Hours DESC, Points DESC LIMIT 6

+--------+-------+
| Points | Hours |
+--------+-------+
|    27  |  8005 |
|    125 |  7200 |
|    200 |  7100 |
|    567 |  1070 |
|    575 |  1050 |
|    450 |  1020 |
+--------+-------+

这是我想要的结果

+--------+-------+
| Points | Hours |
+--------+-------+
|    27  |  8005 |
|    200 |  7100 |
|    125 |  7200 |
|    575 |  1050 |
|    567 |  1070 |
|    450 |  1020 |
+--------+-------+

点和小时都是正常整数

2 个答案:

答案 0 :(得分:1)

使用FLOOR(Hours / 1000)Hours DIV 1000

SELECT *
FROM  CurrencyUser
ORDER BY Hours DIV 1000 DESC, Points DESC

请注意,投放到UNSIGNED将充当ROUND(),因此7400将位于组7中,但7600位于组8中}。您的样本数据不包含此案例。

运行此测试以查看差异:

set @Hours = 7600;

select cast(@Hours/1000 as unsigned)
     , round(@Hours/1000)
     , floor(@Hours/1000)
     , @Hours DIV 1000

演示:http://rextester.com/DZYZ80148

答案 1 :(得分:0)

SELECT * FROM  CurrencyUser ORDER BY cast(Hours/1000 as int) DESC, Points DESC LIMIT 6;