将逗号分隔的未知元素字符串拆分为 PostgreSQL 11.0 中的多个列
Split a comma separated string of unknown elements to multiple columns in PostgreSQL 11.0
我在 PostgreSQL 中有一个 table,具有以下值。
col1
; substrate
positive allosteric modulator
inducer; substrate
我想用“;”拆分行值成多个列。据我了解,split_part() 函数仅适用于固定数量的值。
如何获得以下输出?
col1 col2 col3
; substrate substrate
positive allosteric modulator positive allosteric modulator
inducer; substrate inducer substrate
谢谢
您可以将其拆分为一个数组,然后访问每个数组元素:
select col1,
elements[1] as col2,
elements[2] as col3
from (
select col1, regexp_split_to_array(col1, '\s*;\s*') as elements
from the_table
) t
我在 PostgreSQL 中有一个 table,具有以下值。
col1
; substrate
positive allosteric modulator
inducer; substrate
我想用“;”拆分行值成多个列。据我了解,split_part() 函数仅适用于固定数量的值。
如何获得以下输出?
col1 col2 col3
; substrate substrate
positive allosteric modulator positive allosteric modulator
inducer; substrate inducer substrate
谢谢
您可以将其拆分为一个数组,然后访问每个数组元素:
select col1,
elements[1] as col2,
elements[2] as col3
from (
select col1, regexp_split_to_array(col1, '\s*;\s*') as elements
from the_table
) t