我有两列有城镇列表,我想将城镇分成行
user 1 | [town1,town2,town3]
我想将其拆分为行:
user 1 | town 1
user 1 | town 2
user 1 | town 3
答案 0 :(得分:4)
答案 1 :(得分:1)
假设列名为user_name
和towns
,您可以执行以下操作:
select t1.user_name, t2.town
from the_table t1
cross join lateral unnest(string_to_array(t1.towns,',')) as t2 (town)
答案 2 :(得分:0)
建立在a_horse_with_no_name的答案上,为什么不能有更简单的东西:
--Get some sample data
WITH test(username, town) as
(
select 'user 1', array['town1', 'town2', 'town3']
UNION
select 'user 2', array['town3', 'town2', 'town5']
)
--The following gives the results you are after.
SELECT username, unnest(town)
FROM test
我完全同意使用不需要的功能,但我不明白交叉连接的原因。
OP,您的" list"是什么数据类型非常重要。存储为。在这里,我假设它存储为ARRAY。如果它被存储为字符串,那么Rahul的答案就可以解决问题。