78

I have two tables in database:

CREATE TABLE items(
 id SERIAL PRIMARY KEY,
 ... some other fields
);

This table contains come data row with unique ID.

CREATE TABLE some_chosen_data_in_order(
 id SERIAL PRIMARY KEY,
 id_items INTEGER[],
);

This table contains array type field. Each row contains values of IDs from table items in specific order. For example: {2,4,233,5}.

Now, I want to get data from table items for chosen row from table some_chosen_data_in_order with order for elements in array type.

My attempt was JOIN:

SELECT I.* FROM items AS I 
JOIN some_chosen_data_in_order AS S ON I.id = ANY(S.id_items) WHERE S.id = ?

Second attempt was subquery like:

SELECT I.* FROM items AS I 
WHERE I.id = ANY 
(ARRAY[SELECT S.id_items FROM some_chosen_data_in_order  WHERE id = ?])

But none of them keep IDs in the same order as in array field. Could you help me, how to get data from items table with correspond with array IDs order from some_chosen_data_in_order table for specific row?

5 Answers 5

132
SELECT t.*
FROM unnest(ARRAY[1,2,3,2,3,5]) item_id
LEFT JOIN items t on t.id=item_id

The above query select items from items table with ids: 1,2,3,2,3,5 in that order.

5
  • 2
    unnest() is a really cool trick! I had a list of ID's from a customer and needed to join against them but wanted to find a way to do it w/o a temp table. You can join (select unnest(ARRAY[1,2,3])) as idlist and it works like a champ. thanks!
    – apinstein
    Apr 3, 2013 at 2:11
  • @apinstein can you please show me the query? still dint get it
    – manocha_ak
    Apr 10, 2013 at 5:18
  • 5
    If you want to join two tables with each other you could for instance use SELECT b.*, a.item_id FROM (SELECT unnest(array_column_in_table_a) item_id FROM table_a) as a RIGHT JOIN table_b b on b.id=a.item_id; Oct 19, 2017 at 11:52
  • 8
    I needed to join to tables, but the above comment didn't work for me, but this did: SELECT * FROM table_a a JOIN table_b b ON b.id = ANY(a.array_column_in_table_a);, thanks to garysieling.com/blog/postgres-join-on-an-array-field
    – Rohmer
    Aug 1, 2018 at 17:02
  • This doesn't match the question, why was it chosen? Feb 17, 2021 at 10:08
50

Probably normalizing your table would be the best advice I can give you.

The int_array contrib module has an idx function that will give you the int's index position in the array. Also there is an idx function on the snippets wiki that works for array's of any data types.

SELECT i.*, idx(id_items, i.id) AS idx
FROM some_chosen_data_in_order s
JOIN items i ON i.id = ANY(s.id_items)
ORDER BY idx(id_items, i.id)
3
  • 1
    It's clearly what I need. Thanx! I'm denormalizing my database due to efficiency reasons.
    – Adiasz
    Mar 24, 2010 at 18:41
  • Works, thanks. The order by clause works with just idx as well. Feb 17, 2021 at 10:22
  • where s.id = ? -- an id number Feb 17, 2021 at 10:44
8
select distinct on (some_chosen_data_in_order.id)
  some_chosen_data_in_order.*,
   array_to_json( array_agg(row_to_json( items))
  over ( partition by some_chosen_data_in_order.id ))
from some_chosen_data_in_order
  left join items on items.id = any (some_chosen_data_in_order.id_items)

enter image description here

0
1
SELECT I.* FROM items AS I 
WHERE I.id IN (SELECT UNNEST(id_items) FROM some_chosen_data_in_order 
(ARRAY[SELECT S.id_items FROM some_chosen_data_in_order  WHERE id = ?])
1
  • 5
    When adding code as answer, then add a bit explanation to your answer for future readers.. May 28, 2015 at 18:23
1

If you don't like functions (https://stackoverflow.com/a/2489805/642096):

SELECT jt.*
FROM (
     SELECT ARRAY_AGG(DISTINCT dupes_column) AS unique_vals
     FROM dupes_table
) q
LEFT JOIN joined_table jt
ON jt.id = ANY(q.unique_vals);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.