简体   繁体   English

使用 Postgresql 将单行中的值拆分为多行

[英]Split value in single row to multiple rows with Postgresql

With Postgresql I have the query:使用 Postgresql 我有疑问:

select '1' , '2', '3';

with result结果

在此处输入图像描述

I would like to have next result:我想要下一个结果:

column
1
2
3

How can I do that?我怎样才能做到这一点?

You could create an array and then use UNNEST():您可以创建一个数组,然后使用 UNNEST():

SELECT UNNEST(ARRAY[1,2,3]);

You are looking for Unpivot , then you can try to use UNION ALL您正在寻找Unpivot ,然后您可以尝试使用UNION ALL

select '1' column
UNION ALL
SELECT '2'
UNION ALL
SELECT '3'

or you can try to use JOIN LATERAL或者你可以尝试使用JOIN LATERAL

SELECT s.*
FROM test t
JOIN LATERAL (VALUES(t.a),(t.b),(t.c)) s(column) ON TRUE

sqlfiddle sqlfiddle

I prefer to use a table function for that:我更喜欢为此使用表格 function :

SELECT CAST(i AS text) FROM generate_series(1, 3) AS i;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM