2017-08-19 13 views
0

行を列から列へと逆にする例がたくさんあります。しかし、これはありません。MySqlのテキスト行から列へ(転置)

行は、このようなURLがあります。

色:黒http://www.example.com/1/http://www.example.com/2/http://www.example.com/3/http://www.example.com/4/

は、私はこのようなURLごとに個別の行にそれらを取得する必要があります:

color   URL 
color: black http://www.example.com/1/ 
color: black http://www.example.com/2/ 
color: black http://www.example.com/3/ 
color: black http://www.example.com/4/ 

は達成する方法はありますこれはMySqlでですか?

+1

あなたのデータは不明です。それはデリミタなしで文字列を乱されていますか?それは複数の列ですか? –

答えて

0

これらは別の列にある場合は、あなたが行うことができます:

select color, url1 as url from t 
union all 
select color, url2 from t 
union all 
select color, url3 from t 
union all 
select color, url4 from t; 

次はうまくいくかもしれない:

を、彼らはその後、1つの台無し列(例により示唆されるように)、のようなものである場合:

select color, 
     concat('http:', substring_index(substring_index(concat(url, 'http:'), 'http:', 2), 'http:', -1)) as url 
from t 
union all 
select color, 
     concat('http:', substring_index(substring_index(concat(url, 'http:'), 'http:', 3), 'http:', -1)) as url 
from t 
where url like 'http:%http:%' 
union all 
select color, 
     concat('http:', substring_index(substring_index(concat(url, 'http:'), 'http:', 4), 'http:', -1)) as url 
from t 
where url like 'http:%http:%http:%'; 

あなたはあなたが持っているURLごとにサブクエリを追加し続けます。

+0

URLはすべてフィールド内の1つの行にあり、無制限の数にすることができます。まず、私はこのフィールドからすべての単一のURLを取得する必要がありますし、それらの行に1つのURLを取得します。 – popkutt