Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres JOIN with unnest

Assume I have following tables:

table: followers_arrays

   id   |  array
--------+---------
    1   | {3,4,5}


table: small_profiles

   id   | username |  pic    
--------+----------+-------
    3   |   aaaa   | abcd
    4   |   bbbb   | abcd
    5   |   cccc   | abcd

I would like to print followers_array with populated data from small_profiles using simple JOINs.

At first, I'm using unnest function like this:

SELECT id, unnest(followers_array) AS elem FROM followers_arrays 

And it gives me about right result:

   id   |  elem  
--------+--------
    1   |    3
    1   |    4
    1   |    5

Now, from my understanding I just need to join this data to small_profiles ON small_profiles.id key like this:

SELECT id, unnest(followers_array) AS elem 
FROM followers_arrays 
JOIN small_profiles ON small_profiles.instagram_id = elem

However it seems that during JOIN, column elem is not created yet because I get following error: ERROR: column "elem" does not exist

Any thoughts how should I rearrange my query? Thanks

like image 898
Rafal Wiliński Avatar asked Nov 01 '15 20:11

Rafal Wiliński


People also ask

How do you use unnest in PostgreSQL?

UNNEST() function. This function is used to expand an array to a set of rows. Syntax: unnest(anyarray) Return Type: setof anyelement . PostgreSQL Version: 9.3 . Example: PostgreSQL UNNEST() function. Code: SELECT unnest(ARRAY[1,2]); Sample Output: unnest ----- 1 2 (2 rows) Previous: STRING_TO_ARRAY function Next: Introduction to JOIN 

How to join multiple rows in an array in PostgreSQL?

In PostgreSQL, the INTERSECT operator can effectively do this for two different sets of rows. However, there is no counterpart for arrays. Likewise, the UNION operator joins 2 pairs of rows; however, there is nothing comparable for arrays. The UNNEST method seems to be the secret to all of this.

How to convert array to table-like structure in PostgreSQL?

By using unnest function, we have no need to use cross join or generate series function to convert an array into the table like structure. We have simply using unnest function with an array. We have also used an array for number or text to convert the array into a table-like structure in PostgreSQL.

How do I insert multiple records into a Postgres table?

All the examples in this article assume a database schema that looks like: To insert many records into a Postgres table in one go, the most efficient method is to provide each column as a separate array and then use UNNEST to construct the rows to insert. Notice that you're only passing 2 parameters, no matter how many rows you want to insert.


2 Answers

That is bad design but here is your answer:

select f.id, f.follower, s.username, s.pic
from
    (
        select id, unnest("array") as follower
        from followers_arrays
    ) f
    inner join
    small_profiles s on f.follower = s.id
like image 168
Clodoaldo Neto Avatar answered Sep 26 '22 01:09

Clodoaldo Neto


I prefer to use Common Table Expressions over Subqueries:

WITH unnested_arr_1 AS (
    SELECT unnest(ARRAY[1, 2, 3]) array_1_item
)
,unnested_arr_2 AS (
    SELECT unnest(ARRAY[2, 3, 4]) array_2_item
)
SELECT  *
FROM    unnested_arr_1 arr1
    FULL OUTER JOIN unnested_arr_2 arr2 ON arr1.array_1_item=arr2.array_2_item

produces:

array_1_item |array_2_item |
-------------|-------------|
1            |[NULL]       |
2            |2            |
3            |3            |
[NULL]       |4            |

If joining only unnested arrays, then the above query can be simplified as follows:

SELECT * 
FROM   unnest(
    ARRAY[1, 2, 3]
   ,ARRAY[2, 3, 4]
) as U(array_1_item , array_2_item );
like image 44
isapir Avatar answered Sep 25 '22 01:09

isapir