How to split two columns array into rows in postgresql?

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

How to split two columns array into rows in postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 8 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.

Related Threads:

How to count unique values across two columns with certain types in postgresql?
How to limit the total columns and rows in mysql
How to split string in postgresql?
How to split the text into two parts in d3.js?
How to split a string into an array in PHP?
How to conditionally group by two different columns in oracle?