我的表格(inf)有两列id_student
和hobbies
,如下所示:
ID_student = 1
Hobbies = "music, cooking, bassguitar"
我希望通过将它们分成三列包含以下列的另一个表(爱好)来复制爱好:
ID_student hobby1 hobby2 hobby3
1 music cooking bassguitar
我怎么能在Postgres写点什么?
答案 0 :(得分:1)
有很多方法可以做到这一点。 一种方法是使用string_to_array函数:
INSERT INTO hobbies (id, hobby1, hobby2, hobby3)
SELECT id,hobbies_array[1],hobbies_array[2],hobbies_array[3] FROM
(
SELECT id,string_to_array(hobbies,',') AS hobbies_array
FROM inf
) AS foo;