2016-04-19 17 views
0

私はPostgreSQLにテーブルを持っています。フィールドの1つに複数の文字列がある場合は、この文字列を別の行に配置します。 私が使用:複数行の行を別の行に分割する

id lng lat descriptions 
1 0.98 51 walk, wedding, bus 

をし、このような別のテーブルに挿入:regexp_split_to_array()

id lng lat descriptions 
1 0.98 51 walk 
1 0.98 51 wedding 
1 0.98 51 bus 

答えて

1

使用unnest()

WITH tb(id,lng,lat,descriptions) AS (VALUES 
    (1,0.98,51,'walk, wedding, bus') 
) 
SELECT id,lng,lat,trim(unnest(regexp_split_to_array(descriptions,','))) AS descriptions FROM tb; 

結果:あなたの時間

id | lng | lat | descriptions 
----+------+-----+-------------- 
    1 | 0.98 | 51 | walk 
    1 | 0.98 | 51 | wedding 
    1 | 0.98 | 51 | bus 
(3 rows) 
+0

おかげでelp :) – Raha1986

+0

regexを使用している場合、 'regexp_split_to_table()'は 'unnest()'と 'regexp_split_to_array()'の組み合わせより速いはずです。しかし、ここで正規表現を使う必要はありません。 'string_to_array()'は 'regexp_split_to_array()'より高速です。 –

関連する問題