如何在MySQL中使用下划线对数据进行排序

时间:2019-07-12 09:28:34

标签: php mysql

下划线中包含的数据如何排序?

图片上有说明,

enter image description here

谢谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您想要自然排序。

<?php

$items =
[
'ABC_1_1',
'ABC_1_3',
'ABC_1_10',
'ABC_1_11',
'ABC_1_5',
'ABC_1_7'
];

sort($items, SORT_NATURAL);
var_dump($items);

输出:

array(6) {
    [0]=>
    string(7) "ABC_1_1"
    [1]=>
    string(7) "ABC_1_3"
    [2]=>
    string(7) "ABC_1_5"
    [3]=>
    string(7) "ABC_1_7"
    [4]=>
    string(8) "ABC_1_10"
    [5]=>
    string(8) "ABC_1_11"
  }

答案 1 :(得分:1)

您可以在下面尝试-

select id_code
from tablename
order by 
substring_index(substring_index(id_code,'_',-2),'_',1),substring_index(id_code,'_',-1)

答案 2 :(得分:0)

您可以在MYSQL中尝试拆分字符串,并按部分排序:

SELECT SUBSTRING_INDEX(ID_DATA, '_', 1) as part1,
       SUBSTRING_INDEX(SUBSTRING_INDEX(ID_DATA, '_', 2), '_', -1) as part2,
       SUBSTRING_INDEX(ID_DATA, '_', -1) as part3,
FROM table
ORDER BY part1, part2, part3