繁体   English   中英

在MySQL中,数值将值按字符串排序

[英]Numeric sort the values as Strings in MySQL

我想知道当数字是字符串时是否有一种方法可以进行数字排序。 看一个例子:

查询:

SELECT * FROM t_tables t由id_string asc排序

这是数据库中的列表:

13
4
6
8
10
1
3
2
5
12
7
9
11

我需要以这种方式提取它:

1
2
3
4
5
6
7
8
9
10
11
12
13

但是结果我得到了这个:

1
10
11
12
13
2
3
4
5
6
7
8
9

如果您知道列中始终有数字值,则可以使用CAST()运算符。 也看看这个问题

SELECT * FROM t_tables t ORDER BY CAST(id_string AS UNSIGNED) ASC
SELECT * FROM t_tables t order by cast(id_string as signed integer) asc;

希望能帮助到你:

SELECT * FROM t_tables t order by cast(id_string as unsigned)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM