STUDY WHILE YOU ARE BORING


  • 首頁

  • 搜索

  • 技术

  • Tags

  • 关于我

[MYSQL]让字符串类型的数字按照正确的姿态排序

發表於 June 2, 2015   |   作者: Tim   |   分类: 技术   |   评论

今天排序遇到了个问题,如果数据类型是 varchar 的数字,在排序的过程中,出来并不是我们想要的效果。
例如:

select * from table order by field_varchar ;
field_varchar
0
1
11
100
2
22
3
33

在网上查找了一圈,比较常见的做法是:

select * from table order by field_varchar+0;

转换为数字

field_varchar
0
1
2
3
11
22
33
100

点进来看的都是有求知欲的,所以对的,故事还没有完。
如果 这个字段里面有 空字符窜 或者 null。上面的方法还不够完美。

会出现下面的情况

field_varchar
0
空
1
2
3
11
22
33
100

因为空字符窜+0 是0, 0+0 也是0 ,这个时候就会出现不稳定的 0 和空 上下跳动的情况。
那么真正争取的姿态是怎么样的呢?

请看大屏幕

select * from table order by length(field_varchar),field_varchar;
mysql, 排序, varchar, number
Powerdesigner 生成增量sql脚本
在网页上,让回车键(enter)模拟Tab键的效果

钜添

54 文章
2 分類
1 页面
GitHub 日记技术
php apache centos macos mysql https shadowsocks mac 微信 ups svn javascript outline accesskey Chrome Extension letsencrypt ssl free certification certbo wildcard renew cloudflare pgsql 归档 binlog isset array_key_exists redmine axure
© 2022 钜添   |   文章 RSS     |  登录
由 Typecho 强力驱动
主題 - NexT
Send message encrypted and private - Msg2
Build your Under Construction Page without hosting - UnderConstruct.IO