如何对包含数字的字符串字段进行排序



我有一个表'orders'。它喜欢以下内容:

table_number(字符串( people_number
1 3
2B 4
A1 3
B2 4
C1 3
1A 4
10 3
2 4
6 4
CA7 4
TB89 4
T85 4
CA78 4
B239 4
E9 4

您可以转换为签名,同时像一样排序

ORDER BY 1-SIGN(CAST(table_number AS SIGNED)), -- check whether castable which will have the precedence after applying 1-SIGN(...)
CAST(table_number AS SIGNED)          -- sort each group among themselves

或者更短的版本可能是

ORDER BY 1-SIGN(table_number+0), table_number+0

编辑(考虑新编辑的数据集(:您可以使用以下排序样式

ORDER BY  1-SIGN(table_number+0), -- check whether castable(eg.the values starting with an integer) which will have the precedence after applying 1-SIGN(...)
table_number+0,  -- the integer part extracted from the whole values those start with a non-integer 
table_number     -- the values starting with a non-integer

演示

最新更新