问题:
对mysql数值字符串类型进行排序,在默认情况下使用order by 字段名称 desc/asc 进行排序的时候,mysql进行的排序规则是按照ASCII码进行排序的,并不会自动的识别出这些数据是数值。
如:
L1,L2,L3…L10,L11排序,按照常规的 desc/asc 进行排序,结果为:
L1,L10,L11,L2,L3…
table:
mysql中varchar字符串按照数字排序
执行sql:select * from test order by score asc;
结果为:
mysql中varchar字符串按照数字排序
期望:按照L1,L2,L3…L10,L11…的顺序排序

解决:

  • 方法1:select * from test order by score-0 asc;

  • 方法2:select * from test order by cast(score as signed ) asc;

  • 方法3:select * from test order by convert(score,signed) asc;

  • 方法4(亲测有效):select * from test order by substr(score from 2)-0 asc;

以上方法,查询结果为:
mysql中varchar字符串按照数字排序

问题解决!

相关文章:

  • 2021-07-13
  • 2022-01-06
  • 2022-12-23
  • 2022-12-23
  • 2021-04-12
  • 2021-08-15
  • 2021-05-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-27
  • 2022-12-23
相关资源
相似解决方案