Sql 如何以字母数字值对数据进行排序

Sql 如何以字母数字值对数据进行排序,sql,Sql,我有这些值:d1、d45、d79、d33、d100 我想从表中按升序对这些变量进行排序 获取输出的查询是什么: d1 d33 d45 d79 d100 对不起,根本不是SQL答案。:)对于只有一个字母的变体,按长度和字母顺序排列。您需要的是“自然排序”。有关Microsoft SQL Server 2005,请参阅。对于其他语言,请参见(例如)。如果您可以保证模式为/\w\d+/ 在研究生阶段: select foo from bar order by cast(substring(foo f

我有这些值:
d1、d45、d79、d33、d100

我想从表中按升序对这些变量进行排序

获取输出的查询是什么:

d1
d33
d45
d79
d100

对不起,根本不是SQL答案。:)对于只有一个字母的变体,按长度和字母顺序排列。

您需要的是“自然排序”。有关Microsoft SQL Server 2005,请参阅。对于其他语言,请参见(例如)。

如果您可以保证模式为/\w\d+/

在研究生阶段:

select foo from bar order by cast(substring(foo from 2) as int)
…对于其他SQL风格,也将存在类似的问题。昂贵的头脑

编辑:androids解决方案看起来也不错:

..order by char_length(foo),foo

如果我们可以假设数据值仅包含字母d和数字值,那么您也可以使用:

select column from YourTable
order by convert(int, replace(column, 'd', ''))
如果它包含任何其他字母,则此方法将很快变得不可用:

select column from YourTable
order by convert(int, 
        replace(replace(replace(replace(replace(
            column, 'a', ''),
                'b', ''),
                'c', ''),
                'd', ''), 
                'e', '')
        )

什么数据库?MySQL、sql server、oracle等等。。。?