I am trying to sort a ranking list of sports players, all of which have a ranking position. However, some are new on tour, so their ranking is 0. If I simply order by position ASC the 0's will appear first, I would like them to appear after the rest. I have tried something like:
我正在尝试排序体育运动员的排名列表,所有这些都有排名位置。然而,有些是巡回赛中的新手,因此他们的排名是0.如果我只是按位置ASC排序,0将首先出现,我希望它们出现在其余的之后。我尝试过类似的东西:
SELECT * FROM rankings WHERE season='$season' ORDER BY CASE position = 0 THEN 999, ELSE position ASC
2 个解决方案
#1
2
You can handle it strictly in the ORDER BY
clause
您可以在ORDER BY子句中严格处理它
SELECT *
FROM rankings
WHERE season='$season'
ORDER BY (position=0) ASC, position ASC
#2
0
try this:
SELECT *, (CASE position WHEN 0 THEN 999 ELSE position) AS custom_sort FROM rankings WHERE season='$season' ORDER BY custom_sort ASC
#1
2
You can handle it strictly in the ORDER BY
clause
您可以在ORDER BY子句中严格处理它
SELECT *
FROM rankings
WHERE season='$season'
ORDER BY (position=0) ASC, position ASC
#2
0
try this:
SELECT *, (CASE position WHEN 0 THEN 999 ELSE position) AS custom_sort FROM rankings WHERE season='$season' ORDER BY custom_sort ASC