我正在处理sql查询,我想根据数字对字符串进行排序。
我有一个列(列名是name)表,其中有多个字段。使用“按名称排序”时,将按以下方式打印:
hello_world
hello_world10
hello_world11
hello_world12
hello_world13
hello_world14
hello_world15
hello_world4
hello_world5
对于上面的查询,我使用了order by name;但它似乎不是根据数字印刷的。
问题陈述:
我想知道我需要写什么sql查询,或者我需要在上面的sql查询中做什么更改,以便它根据数字打印所有内容,o/p应该是这样的:
hello_world
hello_world4
hello_world5
hello_world10
hello_world11
hello_world12
hello_world13
hello_world14
hello_world15
5条答案
按热度按时间bqf10yzr1#
我们可以用替换和转换的方法来订购。我尝试了以下查询
生成示例数据
编辑不替换列的查询,
kuuvgm7e2#
我认为对于这种特殊情况(所有值都有相同的前缀)最简单的解决方案是:
ds97pgxw3#
如果需要数字排序,则需要创建一个数字值进行排序。
当前您有字符串。
如果模式为true,则可以使用字符串操作的组合来修剪第一个字符(应该只保留数字),然后使用to \u number()转换以进行排序
像这样的
iq3niunx4#
试试这个:
yyyllmsg5#
尽管问题是关于mysql的。
我试过使用sql server。