mysql - SQL按字符串顺序排序

标签 mysql sql

在我的表中有两个文件,一个是id,另一个是strNum。我想像这样订购 strNum

One
Two
Three

我的表格数据

    id  strNum      
------  ------------
     1  Two         
     2  One         
     3  Five        
     4  Nine  

我想在不添加任何额外字段的情况下获得输出。我怎样才能像这样订购

One
Two
Three
Four
Five

最佳答案

你可以使用Case:

select strNum
from A
order by case when strNum='one' then 1
                    when strNum='two' then 2
                    when strNum='three' then 3
                    when strNum='four' then 4
                    when strNum='five' then 5
                    when strNum='six' then 6
                    when strNum='seven' then 7
                    when strNum='eight' then 8
                    when strNum='nine' then 9
                    when strNum='ten' then 10  end

您没有提到您使用的是哪个数据库。这是 PostgreSQL 版本。 参见 SQLFiddle

关于mysql - SQL按字符串顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32293636/

相关文章:

mysql - sql语句中列的顺序会影响查询速度吗?

mysql - 如何使用给定的正则表达式格式检查 sql 查询中用户输入的格式?

javascript - 从数据库中获取数据到js数组

mysql select month include into 2个月

java - 查询中的字符串参数

java - SQL:connection.close - 需要一些练习提示

java - 缓存结果集

mysql、SQL、触发器、多对多

mysql - bash/mysql 更新行计数中的字符串提取

javascript - WordPress 自动完成搜索 : how to connect query file of custom table?