How to split two columns array into rows in postgresql?

by ryan.murray , in category: MySQL , 2 months ago

How to split two columns array into rows in postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 2 months ago

@ryan.murray 

In PostgreSQL, you can use the UNNEST() function to split a two-columns array into rows. Here's an example:


Suppose you have a table called "example_table" with a column called "array_column" that contains a two-columns array, like this:


| array_column | |-------------------| | {"A","1"} | | {"B","2"} | | {"C","3"} |


You can split the array_column into rows using the UNNEST() function like this:

1
2
3
4
5
SELECT
    UNNEST(array_column) as col1,
    UNNEST(array_column) as col2
FROM
    example_table;


This will return the following result:


| col1 | col2 | |-----------|-----------| | A | 1 | | B | 2 | | C | 3 |


This way, you have split the two-columns array into individual rows in PostgreSQL.