用于按数字排序的 SQL - 1,2,3,4 等而不是 1,10,11,12 [英] SQL for ordering by number - 1,2,3,4 etc instead of 1,10,11,12

查看:76
本文介绍了用于按数字排序的 SQL - 1,2,3,4 等而不是 1,10,11,12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按数据库中的数字列进行排序,该列的值为 1-999

I’m attempting to order by a number column in my database which has values 1-999

当我使用

ORDER_BY registration_no ASC

我明白了……

1
101
102
103
104
105
106
107
108
109
11
110
Etc…

所以它似乎是按第一个数字排序,而不是数字.

So it appears to be ordering by the first digit as oppose to the number.

如果我想按值排序,有人知道要使用什么 SQL 吗?所以 1,2,3,4,5,6 等等

Does anyone know what SQL to use if I want to order this by value? So 1,2,3,4,5,6 etc

推荐答案

一种按正整数排序的方法,当它们存储为 varchar 时,是先按长度排序,然后按值排序:

One way to order by positive integers, when they are stored as varchar, is to order by the length first and then the value:

order by len(registration_no), registration_no

这在列可能包含非数字值时特别有用.

This is particularly useful when the column might contain non-numeric values.

注意:在某些数据库中,获取字符串长度的函数可能被称为length() 而不是len().

Note: in some databases, the function to get the length of a string might be called length() instead of len().

这篇关于用于按数字排序的 SQL - 1,2,3,4 等而不是 1,10,11,12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆