varchar类型的字段存储纯数字的排序,如果表字段的类型为varchar型,但是里面存储的是纯数字,怎么实现按照数字的大小来排序?
下面的方法对于 mysql 和oracle都实用
order by 字段+0
order by 字段*1
等等都可以实现
例子:
mysql> select * from goolen order by id;
+------+
| id |
+------+
| 1 |
| 10 |
| 11 |
| 2 |
| 20 |
| 22 |
| 3 |
| 4 |
| 5 |
+------+
9 rows in set (0.00 sec)
mysql> select * from goolen order by id+0;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 10 |
| 11 |
| 20 |
| 22 |
+------+
下面的方法对于 mysql 和oracle都实用
order by 字段+0
order by 字段*1
等等都可以实现
例子:
mysql> select * from goolen order by id;
+------+
| id |
+------+
| 1 |
| 10 |
| 11 |
| 2 |
| 20 |
| 22 |
| 3 |
| 4 |
| 5 |
+------+
9 rows in set (0.00 sec)
mysql> select * from goolen order by id+0;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 10 |
| 11 |
| 20 |
| 22 |
+------+
9 rows in set (0.00 sec)
本文介绍了一种在MySQL和Oracle数据库中针对存储为Varchar类型的纯数字字段进行数值排序的方法。通过简单的数学操作即可实现正确排序,适用于需要按数字大小对文本型数字字段排序的场景。
471

被折叠的 条评论
为什么被折叠?



