這個查詢將您的字符串分成幾部分:
select t.col
, cast(num1 as unsigned) n1
, cast(num2 as unsigned) n2
, cast(num3 as unsigned) n3
, cast(num4 as unsigned) n4
, cast(num5 as unsigned) n5
, cast(num6 as unsigned) n6
from (
select t.col
, SUBSTRING_INDEX(t.col, '-', 1) str
, SUBSTRING_INDEX(t.col, '-', -3) num1
, SUBSTRING_INDEX(t.col, '.', -3) num2
, SUBSTRING_INDEX(t.col, '-', -2) num3
, SUBSTRING_INDEX(t.col, '.', -2) num4
, SUBSTRING_INDEX(t.col, '-', -1) num5
, SUBSTRING_INDEX(t.col, '.', -1) num6
from Table1 t
) t;
--
| col | n1 | n2 | n3 | n4 | n5 | n6 |
|------------------|----|----|----|----|----|----|
| XYZ-1.0-7.0-1 | 1 | 0 | 7 | 0 | 1 | 0 |
| XYZ-1.0-27.0-5.7 | 1 | 0 | 27 | 0 | 5 | 7 |
| XYZ-1.0-20.0-4.6 | 1 | 0 | 20 | 0 | 4 | 6 |
| XYZ-1.0-10.0-2.4 | 1 | 0 | 10 | 0 | 2 | 4 |
http://sqlfiddle.com/#!9/d3770/1
使用這些零件FO的排序結果:
select t.col
from (
select t.col
, SUBSTRING_INDEX(t.col, '-', 1) str
, SUBSTRING_INDEX(t.col, '-', -3) num1
, SUBSTRING_INDEX(t.col, '.', -3) num2
, SUBSTRING_INDEX(t.col, '-', -2) num3
, SUBSTRING_INDEX(t.col, '.', -2) num4
, SUBSTRING_INDEX(t.col, '-', -1) num5
, SUBSTRING_INDEX(t.col, '.', -1) num6
from Table1 t
) t
order by str
, cast(num1 as unsigned)
, cast(num2 as unsigned)
, cast(num3 as unsigned)
, cast(num4 as unsigned)
, cast(num5 as unsigned)
, cast(num6 as unsigned)
http://sqlfiddle.com/#!9/d3770/2
你Ç一個也消除子查詢:
select t.col
from Table1 t
order by SUBSTRING_INDEX(t.col, '-', 1)
, cast(SUBSTRING_INDEX(t.col, '-', -3) as unsigned)
, cast(SUBSTRING_INDEX(t.col, '.', -3) as unsigned)
, cast(SUBSTRING_INDEX(t.col, '-', -2) as unsigned)
, cast(SUBSTRING_INDEX(t.col, '.', -2) as unsigned)
, cast(SUBSTRING_INDEX(t.col, '-', -1) as unsigned)
, cast(SUBSTRING_INDEX(t.col, '.', -1) as unsigned)
http://sqlfiddle.com/#!9/d3770/5
什麼是第一位的* 1.11 *或* 1.2 *? –
訂單將1.0然後1.1等等。它在所有情況下在「 - 」符號後使用。 – steven