• news
  • pics
  • linux
  • windows
  • proxmox
  • game
  • news
  • pics
  • linux
  • windows
  • proxmox
  • game
Home
linux

MySQL中怎么对varchar类型排序问题

在mysql默认order by 只对数字与日期类型可以排序,但对于varchar字符型类型排序好像没有用了,下面我来给各位同学介绍varchar类型排序问题如何解决。

今天在对国家电话号码表进行排序的时候发现了一个有趣的问题,我想让isdcode字段按照由小到大的顺序排序,于是乎我是这样写的

SELECT * FROM gb_country_isdcode ORDER BY isdcode asc

结果如下,发现竟然不是我想要的结果,asc排序是对的呀,于是乎我找呀找,找呀找,终于找到原因了;
isdcode是varcher类型的,如果排序的直接用asc显然是不行的,必须将他转换成int类型然后就可以正常排序了,只要isdcode + 0就可以了
于是乎这样写

SELECT * FROM gb_country_isdcode ORDER BY (isdcode+0) asc

面的呀。好像是想要的那种数据比较大小的了呀。。可是为什么+0就好了呢?

原来,+0后就转换INT类型排序了。这样就可以按照大小排序了。

如果不是电话而是汉字怎么办,汉字排序我们只要进行简单转换即可排序了

在mysql中使用order by对存储了中文信息的字段,默认出来的结果并不是按汉字拼音的顺序来排序,要想按汉字的拼音来排序,需要把数据库的字符集设置为UTF8,然后在order by 时候强制把该字段信息转换成GBK,这样出来的结果就是按拼音顺序排序的。例如:

SELECT * FROM table_name ORDER BY CONVERT(column_name USING gbk);

在mysql中试了一下,结果很令人满意。

结论是:查询的时候,通过convert函数,把查询出来的数据使用的字符集gb2312编码就可以了,然后使用convert之后的中文排序。但是如果真的去把表中字段的字符集改成gb2312,又会涉及到很多编码的问题,页面传值啊, 从数据库中存取啊,很麻烦。只要在查询的时候,指定一下字符集,并不是真的把物理字段改成gb2312,很简单。

Previous article CentOS7添加阿里云yum源
Next article “明日香”紧身胶皮衣,简直就是犯规级别的诱惑!!!

therock

18 Comments

  1. the wicked good ketogenic diet cookbook: easy, whole food keto recipes for any budget
    July 9, 2021 at 2:50 am

    keto coffee cake

  2. keto diet tips
    July 8, 2021 at 7:28 am

    keto egg fast

  3. keto philly cheesesteak
    July 7, 2021 at 2:23 am

    keto diet and hypoglycemia

  4. top 10 free international dating sites
    June 25, 2021 at 11:20 pm

    free dating sitea

  5. free dating site in the usa without payment
    June 24, 2021 at 11:26 pm

    dating simulators free

  6. 100 completely free dating
    June 24, 2021 at 8:52 pm

    100 free asian dating

  7. free dating on line
    June 24, 2021 at 10:50 am

    senior widows and widowers free dating site

  8. free online dating romania
    June 23, 2021 at 9:50 pm

    free dating websites scotland

  9. Usescedge
    June 14, 2021 at 8:43 pm

    cialis 5mg best price

  10. Usescedge
    June 13, 2021 at 4:55 pm

    kamagra instructions

  11. top free dating sites
    June 2, 2021 at 1:04 am

    free no credit card dating websites

  12. free herpes dating personals
    June 1, 2021 at 8:58 pm

    free transvestite dating sites

  13. free dating chat line numbers
    June 1, 2021 at 8:19 pm

    100 percent free christian dating site

  14. free bisexual dating uk
    May 21, 2021 at 12:49 am

    free dating site for africans

  15. indonesian dating free
    May 21, 2021 at 12:29 am

    dating sites for nerds free

  16. free one night stand dating sites
    May 20, 2021 at 7:56 pm

    free lesbian dating sites in chicago

  17. finland dating site free
    May 20, 2021 at 12:52 am

    find free online friendship dating

  18. free dating sites for over 45
    May 19, 2021 at 7:12 pm

    free dating sites for over 45

Meta
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
Categories
  • game
  • linux
  • news
  • pics
  • proxmox
  • windows
Recent Posts
  • proxmox7.3直通显卡
  • Proxmox VE 直通显卡方案及解决N卡Code43
  • Linux系统出现:You have new mail in /var/spool/mail/root 的原因及解决办法
  • debian系统解决中文乱码
  • pve添加git和pvetools
Recent Comments
  • EdgarTot on proxmox7.3直通显卡
  • Ashvem on proxmox7.3直通显卡
  • Tedvem on proxmox7.3直通显卡
  • Jackvem on proxmox7.3直通显卡
  • Miavem on proxmox7.3直通显卡
Archives
  • March 2023 (1)
  • December 2022 (4)
  • November 2022 (6)
  • April 2022 (1)
  • February 2022 (5)
  • January 2022 (2)
  • December 2021 (3)
  • November 2021 (1)
  • October 2021 (2)
  • September 2021 (1)
  • August 2021 (1)
  • July 2021 (8)
  • June 2021 (14)
  • May 2021 (2)
  • April 2021 (2)
  • March 2021 (10)
  • January 2021 (4)
  • December 2020 (4)
  • November 2020 (13)
  • April 2020 (276)
  • March 2020 (1)
  • June 2019 (5)
  • May 2019 (10)
  • December 2015 (1)