gpt4 book ai didi

MySQL - 如何对数字上的字母数字数据进行排序

转载 作者:行者123 更新时间:2023-11-29 19:05:58 25 4
gpt4 key购买 nike

我尝试对 MySQL 中包含字母数字数据的列之一进行排序。数据以字符开头,以数字结尾。

这是我的示例数据集:

insert into test_sort (port) values ('GI 1/0/1');
insert into test_sort (port) values ('GI 1/0/4');
insert into test_sort (port) values ('GI 1/0/10');
insert into test_sort (port) values ('GI 1/0/6');
insert into test_sort (port) values ('GI 1/0/3');
insert into test_sort (port) values ('GI 1/0/12');
insert into test_sort (port) values ('TE 1/1/3');
insert into test_sort (port) values ('GI 1/1/1');
insert into test_sort (port) values ('GI 2/0/11');
insert into test_sort (port) values ('GI 2/0/1');
insert into test_sort (port) values ('GI 2/0/3');
insert into test_sort (port) values ('GI 1/1/2');
insert into test_sort (port) values ('TE 1/1/4');

我尝试得到这个输出:

GI 1/0/1
GI 1/0/3
GI 1/0/4
GI 1/0/6
GI 1/0/10
GI 1/0/12
GI 1/1/1
GI 1/1/2
TE 1/1/3
TE 1/1/4
GI 2/0/1
GI 2/0/3
GI 2/0/11

我尝试按长度、子字符串、二进制、*1 进行排序,但没有成功。

非常感谢您的帮助!

最佳答案

您可以使用如下查询:

SELECT *
FROM test_sort
ORDER BY
SUBSTRING_INDEX(SUBSTRING_INDEX(PORT,'/',1),' ',-1)+0,
SUBSTRING_INDEX(SUBSTRING_INDEX(PORT,'/',2),'/',-1)+0,
SUBSTRING_INDEX(SUBSTRING_INDEX(PORT,'/',3),'/',-1)+0;

测试查询

SELECT t.*,
SUBSTRING_INDEX(SUBSTRING_INDEX(PORT,'/',1),' ',-1)+0 as D1,
SUBSTRING_INDEX(SUBSTRING_INDEX(PORT,'/',2),'/',-1)+0 as D2,
SUBSTRING_INDEX(SUBSTRING_INDEX(PORT,'/',3),'/',-1)+0 as D§
FROM test_sort t
ORDER BY
SUBSTRING_INDEX(SUBSTRING_INDEX(PORT,'/',1),' ',-1)+0,
SUBSTRING_INDEX(SUBSTRING_INDEX(PORT,'/',2),'/',-1)+0,
SUBSTRING_INDEX(SUBSTRING_INDEX(PORT,'/',3),'/',-1)+0;

关于MySQL - 如何对数字上的字母数字数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43501924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com