0

One of the table in postgreSQL has a column of datatype text[]

TableA:
id              uuid
tableb_ids      text[]

TableB:
id              uuid
name            text

Now I need to write a query like:

select * from tableB where id in (select tableb_ids from tableA where id ="xxxx-xxxx-xxxx-xxxx")

I cannot change the schema/table definitions. i.e) I cannot keep many entries for every entry of tableB in tableA. TableA is a complex table.

11
  • Something like SELECT b.* FROM tableb b JOIN tablea a ON b.id = ANY(a.tableb_ids) Commented Nov 11, 2019 at 11:22
  • @404 I get ERROR: operator does not exist: uuid = text[] when I try that way Commented Nov 11, 2019 at 11:40
  • b.id::TEXT = ANY(a.tableb_ids) Commented Nov 11, 2019 at 11:41
  • 1
    @Surya: please do not extend your question once you have an answer to your initial problem. Ask a new question instead. Commented Nov 11, 2019 at 12:15
  • 2
    @404: you should add that as an answer, so it can be accepted and the question can be marked as resolved Commented Nov 11, 2019 at 12:17

1 Answer 1

1

To find records in one table whose record ids are contained in an array in some other table, you can join on the tables:

SELECT b.*
FROM tableb b
INNER JOIN tablea a
    ON b.id::TEXT = ANY(a.tableb_ids)
    AND a.id = 'xxxx-xxxx-xxxx-xxxx'

Another way you could do it:

SELECT b.*
FROM tableb b
WHERE id IN (
    SELECT UNNEST(a.tableb_ids)
    FROM tablea a
    WHERE a.id = 'xxxx-xxxx-xxxx-xxxx'
) x
-- not sure if the aliases are needed in the subquery
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.