我有 3 个字段,例如title、name和size 我正在尝试对字符串列(包含数字)进行排序。
+-----------------+--------------+-------------------+
+----- title -----+---- name ----+-------size--------+
+-----------------+--------------+-------------------+
+ SPR-235 + SPR 235 + 118 x 118 x 43 mm +
+ SPR-355-D + SPR 355 D + 140 x 140 x 41 mm +
+ SPR-355-K + SPR 355 K + 140 x 140 x 41 mm +
+ SPR-415 + SPR 415 + 155 x 155 x 50 mm +
+ SPR-455-K + SPR 455 K + 138 x 138 x 64 mm +
+ SPR-455-D + SPR 455 D + 138 x 138 x 64 mm +
+ SPR-135 + SPR 135 + 60 x 60 x 82 mm +
我使用了这个查询:
SELECT title,name FROM table ORDER BY CAST(SUBSTRING(name,LOCATE(' ',name)+1) AS SIGNED)
它像这样工作正常:
+-----------------+--------------+
+----- title -----+---- name ----+
+-----------------+--------------+
+ SPR-135 + SPR 135 +
+ SPR-235 + SPR 235 +
+ SPR-355-D + SPR 355 D + <-- see
+ SPR-355-K + SPR 355 K + <-- see
+ SPR-415 + SPR 415 +
+ SPR-455-D + SPR 455 D + <-- see
+ SPR-455-K + SPR 455 K + <-- see
但是当我将查询更改为:
SELECT * FROM table ORDER BY CAST(SUBSTRING(name,LOCATE(' ',name)+1) AS SIGNED)
不幸的是,“D”和“K”未能排序。并显示如下:
+-----------------+--------------+-------------------+
+----- title -----+---- name ----+-------size--------+
+-----------------+--------------+-------------------+
+ SPR-135 + SPR 135 + 60 x 60 x 82 mm +
+ SPR-235 + SPR 235 + 118 x 118 x 43 mm +
+ SPR-355-D + SPR 355 D + 140 x 140 x 41 mm + <-- see
+ SPR-355-K + SPR 355 K + 140 x 140 x 41 mm + <-- see
+ SPR-415 + SPR 415 + 155 x 155 x 50 mm +
+ SPR-455-K + SPR 455 K + 138 x 138 x 64 mm + <-- see
+ SPR-455-D + SPR 455 D + 138 x 138 x 64 mm + <-- see
我想先对“D”排序,然后对“K”排序。非常感谢帮助这个问题:)