带 IF 语句的 ORDER BY 子句
ORDER BY Clause with IF Statment
我正在努力处理带有 If 语句的 Order by 子句,而没有 If 语句它工作正常。我的 test
table 中有两个 int
类型的关键点和 timestamp
类型的日期。我正在添加示例数据以供参考
Table Name - test
id points date
------------------------
1 90 2019-12-14 09:01:10
2 10 2019-12-15 09:01:10
3 200 2019-12-16 09:01:10
4 120 2019-12-6 09:01:10
5 606 2019-12-9 09:01:10
所以我的查询
SELECT id, points, date FROM test order by points desc
Returns
id points date
------------------------
5 606 2019-12-9 09:01:10
3 200 2019-12-16 09:01:10
4 120 2019-12-6 09:01:10
1 90 2019-12-14 09:01:10
2 10 2019-12-15 09:01:10
这是预期的结果,但如果我尝试在 If 子句中添加 points
,那么它将按字母顺序对点进行排序
查询
SELECT id, points, date FROM test order by IF(TRUE, points, date) desc
Returns
id points date
------------------------
1 90 2019-12-14 09:01:10
5 606 2019-12-9 09:01:10
3 200 2019-12-16 09:01:10
4 120 2019-12-6 09:01:10
2 10 2019-12-15 09:01:10
如何实现数字降序的结果。
预期结果
id points date
------------------------
5 606 2019-12-9 09:01:10
3 200 2019-12-16 09:01:10
4 120 2019-12-6 09:01:10
1 90 2019-12-14 09:01:10
2 10 2019-12-15 09:01:10
注意:我使用的是MYSQL8.0.21
来自评论:
I want to sort my result on the basis of user input, user can choose, he want to sort the result by points or date field
两列的数据类型不同,因此您需要两个不同级别的排序。假设用户输入作为参数 :order_col
给出,可能取值“points”或“date”:
order by
case when :order_col = 'points' then points end desc,
case when :order_col = 'date' then date end desc
另一种方法是将日期转换为数字,例如使用 unix_timestamp()
:这样数据类型就一致了,单级排序就足够了:
order by case :order_col
when 'points' then points
when 'date' then unix_timestamp(date)
end desc
我正在努力处理带有 If 语句的 Order by 子句,而没有 If 语句它工作正常。我的 test
table 中有两个 int
类型的关键点和 timestamp
类型的日期。我正在添加示例数据以供参考
Table Name - test
id points date
------------------------
1 90 2019-12-14 09:01:10
2 10 2019-12-15 09:01:10
3 200 2019-12-16 09:01:10
4 120 2019-12-6 09:01:10
5 606 2019-12-9 09:01:10
所以我的查询
SELECT id, points, date FROM test order by points desc
Returns
id points date
------------------------
5 606 2019-12-9 09:01:10
3 200 2019-12-16 09:01:10
4 120 2019-12-6 09:01:10
1 90 2019-12-14 09:01:10
2 10 2019-12-15 09:01:10
这是预期的结果,但如果我尝试在 If 子句中添加 points
,那么它将按字母顺序对点进行排序
查询
SELECT id, points, date FROM test order by IF(TRUE, points, date) desc
Returns
id points date
------------------------
1 90 2019-12-14 09:01:10
5 606 2019-12-9 09:01:10
3 200 2019-12-16 09:01:10
4 120 2019-12-6 09:01:10
2 10 2019-12-15 09:01:10
如何实现数字降序的结果。
预期结果
id points date
------------------------
5 606 2019-12-9 09:01:10
3 200 2019-12-16 09:01:10
4 120 2019-12-6 09:01:10
1 90 2019-12-14 09:01:10
2 10 2019-12-15 09:01:10
注意:我使用的是MYSQL8.0.21
来自评论:
I want to sort my result on the basis of user input, user can choose, he want to sort the result by points or date field
两列的数据类型不同,因此您需要两个不同级别的排序。假设用户输入作为参数 :order_col
给出,可能取值“points”或“date”:
order by
case when :order_col = 'points' then points end desc,
case when :order_col = 'date' then date end desc
另一种方法是将日期转换为数字,例如使用 unix_timestamp()
:这样数据类型就一致了,单级排序就足够了:
order by case :order_col
when 'points' then points
when 'date' then unix_timestamp(date)
end desc