如何将数据从一列拆分为另一列的三列 table? SQL

How to split data from one column to three columns in another table? SQL

我有 table (inf),其中有两列 id_studenthobbies,如下所示:

ID_student =  1      
Hobbies =   "music, cooking, bassguitar" 

我想通过将爱好拆分为另一个 table(爱好)来复制爱好,分为三列,其中包含以下列:

ID_student   hobby1   hobby2     hobby3
1            music    cooking    bassguitar

我怎么能在 Postgres 中写出类似的东西?

有很多方法可以做到这一点。 一种方法是使用 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;