Mysql 中的字母数字排序

发布于 2024-09-04 03:29:19 字数 406 浏览 6 评论 0原文

当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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一念一轮回 2024-09-11 03:29:19

我会这样做:

select id, name from Table1 order by names + 0, names;

无需订购:

mysql> select * from alpha;
+---+-------+
| i | name  |
+---+-------+
| 1 | ab001 |
| 2 | ab010 |
| 3 | aa002 |
| 4 | cc001 |
| 5 | cb010 |
| 6 | aaa02 |
+---+-------+
6 rows in set (0.00 sec)

使用我的查询:

mysql> select i, name from alpha order by name + 0, name;
+---+-------+
| i | name  |
+---+-------+
| 3 | aa002 |
| 6 | aaa02 |
| 1 | ab001 |
| 2 | ab010 |
| 5 | cb010 |
| 4 | cc001 |
+---+-------+
6 rows in set (0.00 sec)

I would do this way:

select id, name from Table1 order by names + 0, names;

without ordering:

mysql> select * from alpha;
+---+-------+
| i | name  |
+---+-------+
| 1 | ab001 |
| 2 | ab010 |
| 3 | aa002 |
| 4 | cc001 |
| 5 | cb010 |
| 6 | aaa02 |
+---+-------+
6 rows in set (0.00 sec)

With my query:

mysql> select i, name from alpha order by name + 0, name;
+---+-------+
| i | name  |
+---+-------+
| 3 | aa002 |
| 6 | aaa02 |
| 1 | ab001 |
| 2 | ab010 |
| 5 | cb010 |
| 4 | cc001 |
+---+-------+
6 rows in set (0.00 sec)
随风而去 2024-09-11 03:29:19

假设你的字符串总是以 3 位数字结尾,你可以使用 < code>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 不支持 功能性索引,所以这个查询会比较慢。最好重新设计数据库(例如单独存储数字),以便不需要对函数值进行排序。

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

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

Result:

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

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.

长途伴 2024-09-11 03:29:19

我遇到了一个稍微不同的问题,但第二个例子对我有用。我会投票赞成,但我才刚刚开始。

我正在处理一个 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!

橘亓 2024-09-11 03:29:19

如果前 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文