顺序字母和数字字符串

mbskvtky  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(447)

我有以下数据:

GROUP 1 - FINANCE
GROUP 10 - SALES
GROUP 11 - MARKETING
GROUP 12 - HR
GROUP 2 - OPS

什么时候 ORDERING mysql中的数据,正确地说,它按照上面的顺序把这些数据带回来。但是,是否仍有(在不更改数据的情况下)可以对其进行排序 Numeric 命令?如下

GROUP 1 - FINANCE
GROUP 2 - OPS
GROUP 10 - SALES
GROUP 11 - MARKETING
GROUP 12 - HR

谢谢!

wnrlj8wa

wnrlj8wa1#

由于MySQL8.0,您可以使用 REPLACE 以及 REGEXP_SUBSTRORDER BY 列:

SELECT * 
FROM table_name
ORDER BY CAST(TRIM(REPLACE(REGEXP_SUBSTR(column_name, '(GROUP )([0-9]+)'), 'GROUP', '')) AS UNSIGNED)
knsnq2tg

knsnq2tg2#

添加 ORDER BY CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(column, ' ', 2), ' ', -1) AS UNSIGNED) 条款应该有帮助。
筑巢 SUBSTRING_INDEX 具有正确参数的函数可以拆分和提取字符串部分。在这种情况下,将提取“group”中第一个字符串部分之后的数字部分。
查询

SELECT
 records.data
FROM (

SELECT 
 "GROUP 1 - FINANCE" AS data
UNION 
SELECT 
 "GROUP 10 - SALES" AS data
UNION 
SELECT 
 "GROUP 11 - MARKETING" AS data 
UNION 
SELECT 
 "GROUP 12 - HR" AS data  
UNION 
SELECT 
 "GROUP 2 - OPS" AS data   
) AS records
ORDER BY 
 CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(records.data, ' ', 2), ' ', -1) AS UNSIGNED)

请参见演示https://www.db-fiddle.com/f/sgquetkadjihra3bg3myca/0

相关问题