Mysql 中的字母数字排序
当mysql中包含字母数字字符的列时,如何仅进行数字排序?
列(名称)是唯一字段。
我的表包含记录,
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
时,我仅按字母字符排序结果。我如何得到这个?
How can i make only numeric order by when the column containing alphanumeric characters in mysql ?
column (name) is unique field.
my table contains the records,
id name
1 ab001
2 ab010
3 aa002
4 ac004
5 ba015
6 ba006
7 aa005
8 ac003
The results must be like this,
id name
1 ab001
3 aa002
8 ac003
4 ac004
7 aa005
6 ba006
2 ab010
5 ba015
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会这样做:
无需订购:
使用我的查询:
I would do this way:
without ordering:
With my query:
假设你的字符串总是以 3 位数字结尾,你可以使用 < code>RIGHT:
结果:
MySQL 不支持 功能性索引,所以这个查询会比较慢。最好重新设计数据库(例如单独存储数字),以便不需要对函数值进行排序。
Assuming your strings always end with 3 digits you could use
RIGHT
:Result:
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.
我遇到了一个稍微不同的问题,但第二个例子对我有用。我会投票赞成,但我才刚刚开始。
我正在处理一个 varchar 列,它可以包含任何类型的信息,但无论其中包含什么内容,它都需要正确排序:
所以我的列可以包含以下信息:
AB
光盘
EF
GH
或
01
02
03
04
Xavier 的答案导致当信息全部按字母顺序时按字母顺序排列,或者当信息是数字时按数字顺序排列。
谢谢
谢谢!
I had a slightly different issue, but the second example worked for me. I'd vote it up, but I just got started here.
I was dealing with a single varchar column which could contain any kind of information, but it needed to sort properly no matter what was in it:
So my column could contain info such as:
AB
CD
EF
GH
or
01
02
03
04
The answer from Xavier caused it to be in alphabetical order when the information was all alphabetical, or numeric when the info was numeric.
Thanks
Thanks!
如果前 X 个字符为字母,其余为数字,并且字母字符各不相同,则最好更改结构以将此数据放入两列中 - 一列用于首字母,一列用于数字。现在,您不必每次订购时都进行转换,只需先在字母列上订购,然后在数字列上订购即可。重新设计结构确实是更好的方法,特别是如果您经常订购或拥有大型数据集。
您甚至可以将当前字段保留为 PK,并让触发器分离到两个新字段。这样,数据就会采用一种在输入或更新时可以自动排序的格式,所有当前代码仍然有效,但您可以重构以使用性能更高的排序。
If you have the first X characters as letters and the rest as numbers and the alpha characters vary, you are far better off changing your structure to put this data in two columns - one for the intital letters and one for the numbers. Now you don't have to do coanversions every time you want to order, you just order on the letter columns first, then the numeric column. A redesign of your structure is truly the better way to do this espcially if you wil be ordering often or if you have a large dataset.
You could even leave the current field as the PK and have a trigger separate out to the two new fields. This way, the data is put in a format that can be ordered automatically when it is entered or updated, all current code will still work, but you can refactor to use more performant ordering.