2016-05-04 13 views
0

英数字データをmysqlで並べ替える必要があります。私のデータは、次のような典型的なタイプです:MySqlによる英数字データの並べ替え

XYZ-1.0-7.0-1 
XYZ-1.0-27.0-5.7 
XYZ-1.0-20.0-4.6 
XYZ-1.0-10.0-2.4 
----------------- many more data in this format ------------ 

私はグーグルでリンクを見つけましたが、何も動作していません。

私は出力として、このデータが欲しい:

XYZ-1.0-7.0-1 
XYZ-1.0-10.0-2.4 
XYZ-1.0-20.0-4.6 
XYZ-1.0-27.0-5.7 
+0

* 1.11 *または* 1.2 *最初は何が来ますか? –

+0

注文は1.0から1.1になります。それは " - "記号の後のすべての場合に使用されます。 – steven

答えて

0

から.-を削除します値とソート。

クエリ

SELECT * FROM your_table_name 
ORDER BY CAST((
    REPLACE(REPLACE(
     RIGHT(your_column_name, LENGTH(your_column_name) - 4), '.', ''), '-', '')) 
AS UNSIGNED); 

SQL Fiddle Demo

+0

1つの値が( 'XYZ-1.1-7.0-1') – steven

+0

の場合、動作しません。@steven:値 'XYZ-1.1-7.0-1'を追加しました。このフィドルをチェックしてください。 http://sqlfiddle.com/#!9/8aec82/1 – Wanderer

+0

"XYZ-1.1-7.0-1"を追加すると、下部に表示されます。 – steven

0

コンピュータがあなたの助けなしであなたが自動にやりたいことはできません。この値を並べ替えるには、文字列の形式であるクエリを「教える」必要があります。形式が常に "XXX-9.9-9.9-9.9"の場合、文字列を値に分割して並べ替えるクエリを作成する必要があります。 のような何か:

SELECT * FROM your_table_name ORDER BY SUBSTRING(col,3), substring(col,5,2), substring(col,5,2), substring(col,9,2) 

他の問題は、あなたの数値は同じ文字列の形式で、常にではないということです。例えば、あなたは第三部に持っている「7.0」と「27.0」。値が "07.0"と "27.0"のどこで有効になるかは、これらの値を数値に変換する必要があります。 これが役立つことを願っています。

0

このクエリでは、部品にあなたの文字列を分割します:

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

あなたはc:

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 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

+0

「XYZ-1.1-7.0-1」を追加した場合、下部に表示されます。 – steven

+0

@steven - OK、私はパターンが私が思ったほど明確ではないことに気付きました。 「XYZ-1.1-7.0-1」には最後の番号がありません。最後のポイント( '.')では、私の解決策は機能しません。ポイントを右から左に数えるからです。実際にあなたのパターンは何ですか? –

+0

これは私のパターンです。 "と - "で構成されています。しかし、数字の任意の形式で。 – steven

関連する問題