Mysql中的字母数字顺序

编程入门 行业动态 更新时间:2024-10-26 16:23:32
本文介绍了Mysql中的字母数字顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当mysql中包含字母数字字符的列时,如何仅按数字顺序排序?

How can i make only numeric order by when the column containing alphanumeric characters in mysql ?

列(名称)是唯一字段.

column (name) is unique field.

我的表包含记录,

id name 1 ab001 2 ab010 3 aa002 4 ac004 5 ba015 6 ba006 7 aa005 8 ac003

结果必须是这样

id name 1 ab001 3 aa002 8 ac003 4 ac004 7 aa005 6 ba006 2 ab010 5 ba015

当我尝试查询Select * from test order by name时,我仅按字母字符获取结果顺序.我怎么得到这个?

When I am trying this query Select * from test order by name, I am getting the results order by alpha characters only. How do I get this ?

推荐答案

假定您的字符串始终以3位数字结尾,则可以使用 RIGHT :

Assuming your strings always end with 3 digits you could use RIGHT:

SELECT id, name FROM Table1 ORDER BY RIGHT(name, 3);

结果:

1, 'ab001' 3, 'aa002' 8, 'ac003' 4, 'ac004' 7, 'aa005' 6, 'ba006' 2, 'ab010' 5, 'ba015'

MySQL不支持功能索引,因此此查询将相对较慢.最好重新设计数据库(例如,单独存储数字),这样就不必对函数的值进行排序.

MySQL doesn't have support for functional indexes, so this query will be relatively slow. It would be better to redesign your database (e.g. store the number separately) so that ordering on the value of a function isn't necessary.

更多推荐

Mysql中的字母数字顺序

本文发布于:2023-11-30 10:01:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字母   顺序   数字   Mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!