我需要一个SQL语句,它首先按1000 Hours
步的间隔进行排序,然后按DESC顺序对Points
进行排序。
我无法弄清楚如何在此示例中使用Case
或Between
/ 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 |
+--------+-------+
点和小时都是正常整数
答案 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
答案 1 :(得分:0)
SELECT * FROM CurrencyUser ORDER BY cast(Hours/1000 as int) DESC, Points DESC LIMIT 6;