MySQL按字母顺序排列

时间:2012-08-23 17:50:12

标签: mysql sql-order-by collation

我有M1 M3 M4 M14 M30 M40等字符串(字母后面的任何int 2-3位) 当我按“ORDER BY name”返回时:

M1, M14, M3, M30, M4, M40

我想要的时候:

M1, M3, M4, M14, M30, M40 它将整个事物视为字符串,但我想将其视为字符串+ int

有什么想法吗?

7 个答案:

答案 0 :(得分:12)

您可以在ORDER BY中使用SUBSTR和CAST AS UNSIGNED / SIGNED:

SELECT * FROM table_name ORDER BY
    SUBSTR(col_name FROM 1 FOR 1),
    CAST(SUBSTR(col_name FROM 2) AS UNSIGNED)

答案 1 :(得分:3)

如果字符串的开头可以有多个字符,例如'M10', 'MTR10', 'ABCD50', 'JL8', etc...,则基本上必须从数字的第一个位置获取名称的子字符串。

不幸的是,MySQL不支持那种REGEXP操作(只返回一个布尔值,而不是实际匹配)。

您可以使用此解决方案来模拟它:

SELECT   name
FROM     tbl
ORDER BY CASE WHEN ASCII(SUBSTRING(name,1)) BETWEEN 48 AND 57 THEN
                   CAST(name AS UNSIGNED)
              WHEN ASCII(SUBSTRING(name,2)) BETWEEN 48 AND 57 THEN
                   SUBSTRING(name,1,1)
              WHEN ASCII(SUBSTRING(name,3)) BETWEEN 48 AND 57 THEN
                   SUBSTRING(name,1,2)
              WHEN ASCII(SUBSTRING(name,4)) BETWEEN 48 AND 57 THEN
                   SUBSTRING(name,1,3)
              WHEN ASCII(SUBSTRING(name,5)) BETWEEN 48 AND 57 THEN
                   SUBSTRING(name,1,4)
              WHEN ASCII(SUBSTRING(name,6)) BETWEEN 48 AND 57 THEN
                   SUBSTRING(name,1,5)
              WHEN ASCII(SUBSTRING(name,7)) BETWEEN 48 AND 57 THEN
                   SUBSTRING(name,1,6)
              WHEN ASCII(SUBSTRING(name,8)) BETWEEN 48 AND 57 THEN
                   SUBSTRING(name,1,7)
         END,
         CASE WHEN ASCII(SUBSTRING(name,1)) BETWEEN 48 AND 57 THEN
                   CAST(SUBSTRING(name,1) AS UNSIGNED)
              WHEN ASCII(SUBSTRING(name,2)) BETWEEN 48 AND 57 THEN
                   CAST(SUBSTRING(name,2) AS UNSIGNED)
              WHEN ASCII(SUBSTRING(name,3)) BETWEEN 48 AND 57 THEN
                   CAST(SUBSTRING(name,3) AS UNSIGNED)
              WHEN ASCII(SUBSTRING(name,4)) BETWEEN 48 AND 57 THEN
                   CAST(SUBSTRING(name,4) AS UNSIGNED)
              WHEN ASCII(SUBSTRING(name,5)) BETWEEN 48 AND 57 THEN
                   CAST(SUBSTRING(name,5) AS UNSIGNED)
              WHEN ASCII(SUBSTRING(name,6)) BETWEEN 48 AND 57 THEN
                   CAST(SUBSTRING(name,6) AS UNSIGNED)
              WHEN ASCII(SUBSTRING(name,7)) BETWEEN 48 AND 57 THEN
                   CAST(SUBSTRING(name,7) AS UNSIGNED)
              WHEN ASCII(SUBSTRING(name,8)) BETWEEN 48 AND 57 THEN
                   CAST(SUBSTRING(name,8) AS UNSIGNED)
         END

这将首先按字符串的字符部分排序,然后是字符串的提取数字部分,只要在字符串的开头有< = 7个字符。如果您需要更多内容,可以将其他WHEN链接到CASE语句。

答案 2 :(得分:1)

您可以使用:

order by name,SUBSTRING(name,1,LENGTH(name)-1)

答案 3 :(得分:1)

我无法解决我的问题,即如下所示排序MLS数字:

V12345 V1000000 V92832

问题是V1000000的价值并不高于其余部分,即使它更大。

使用它解决了我的问题:

ORDER BY CAST(SUBSTR(col_name FROM 2) AS UNSIGNED) DESC

刚删除了SUBSTR(col_name FROM 1 FOR 1)

答案 4 :(得分:0)

它将数字和字母分开。

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(col,'1', 1), '2', 1), '3', 1), '4', 1), '5', 1), '6', 1)
, '7', 1), '8', 1), '9', 1), '0', 1) as new_col  
FROM table group by new_col; 

答案 5 :(得分:0)

尝试使用SUBSTR删除该字符。然后使用ABS从字段中获取绝对值:

SELECT * FROM table ORDER BY ABS(SUBSTR(field,1));

答案 6 :(得分:0)

我在项目中使用的另一种方法是:

SELECT * FROM table_name ORDER BY LENGTH(col_name) DESC, col_name DESC LIMIT 1