duanji5746 2013-07-16 08:12
浏览 22
已采纳

在mysql中排序字母数字? [关闭]

040, 044P, 041BL, 041W, 041PB

^^ This is the order it is coming out in by using Order By clause.

I think this is how it should appear instead:

040, 041BL, 041PB, 041W, 044P

I know similar questions must have been asked before, but I still can't figure out anything!

Edit: After X.L.Ant's comment, I realized my mistake. Therefore, simple order by clause is working for the test case given above. However, the case is still complicated if the number of digits is not always going to be 3 as GolezTrol mentioned. What should one do in that case?

  • 写回答

2条回答 默认 最新

  • douwei4370 2013-07-16 08:44
    关注

    Try:

    SELECT string,
           @num := CONVERT(string, signed)                            AS num_part,
           Substring(Trim(LEADING '0' FROM string), Length(@num) + 1) AS rest_of_string
    FROM   table1
    ORDER  BY num_part,
              rest_of_string  
    

    This way, the numbers will still be ordered by their numerical value (the leading 0s not being taken into account).

    See fiddle.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分