MySQL ORDER BY Customized

前提是你 提交于 2019-12-12 02:16:48

问题


I have these rows y DB and I would like order by but in the below order with caracters and number. The colum Score is a varchar. WINNER and LOSER are in Score colum also.

Score

WINNER
100+
100
90
80+
80
50
LOSER

回答1:


This approach converts the score value to a number when ordering. I tried it with your data, then with your data plus additional values, and it worked both times:

SELECT score
FROM myTable
ORDER BY
  CASE
    WHEN score = 'WINNER' THEN 100000
    WHEN score = 'LOSER' THEN -100000
    WHEN score LIKE '%+' THEN score * 100 + 99
    ELSE score * 100
 END DESC

The conversion is as follows:

  • WINNER = 100,000
  • LOSER = -100,000
  • number+ = number * 100 + 99
  • number = number


来源:https://stackoverflow.com/questions/17045230/mysql-order-by-customized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!