gpt4 book ai didi

php - MySQL 字母数字值的自然排序

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

我想对以下数据的字母 + 字母数字值进行排序:

In-Direct Labor
Level 1
Level 10
Level 11
Level 12
Level 13
Level 14
Level 15
Level 16
Level 17
Level 2
Level 3
Level 4
Level 5
Level 6
Level 7
Level 8
Level 9
Risers Risers
Roof/Penthouse
Site

我尝试了以下解决方案,但它没有按照我想要的方式返回结果

http://www.copterlabs.com/blog/natural-sorting-in-mysql/

In-Direct Labor
Level 1
Level 2
Level 3
Level 4
Level 5
Level 6
Level 7
Level 8
Level 9
Level 10
Level 11
Level 12
Level 13
Level 14
Level 15
Level 16
Level 17
Risers Risers
Roof/Penthouse
Site

最佳答案

可能是提取第一个词并将第二个词视为数字顺序子句:

select *
from ab
order by
substring(col,1, case when locate(' ',col) = 0 then 100 else locate(' ',col) end ),
substring(col,case when locate(' ',col) = 0 then 100 else locate(' ',col) end ) + 0;

-- col contains your field.

+-----------------+
| col |
+-----------------+
| In-Direct Labor |
| Level 1 |
| Level 2 |
| Level 3 |
| Level 4 |
| Level 5 |
| Level 6 |
| Level 7 |
| Level 8 |
| Level 9 |
| Level 10 |
| Level 11 |
| Level 12 |
| Level 13 |
| Level 14 |
| Level 15 |
| Level 16 |
| Level 17 |
| Risers Risers |
| Roof/Penthouse |
| Site |
+-----------------+
21 rows in set (0.01 sec)

关于php - MySQL 字母数字值的自然排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23967185/

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