0

I want to join two table, with first table contain an array which is primary keys of second table in JSON format

1
  • 1
    Please edit your question (by clicking on the edit link below it) and add some sample data and the expected output based on that data as formatted text. See here for some tips on how to create nice looking text tables. (edit your question - do not put code or additional information in comments) Commented Feb 24, 2021 at 7:31

1 Answer 1

1

demo:db<>fiddle

You can use the ANY function:

SELECT
    *
FROM a
JOIN b ON a.id = ANY(ids)

Edit:

demo:db<>fiddle

If you have jsonb arrays, you can use the @> operator. Note, that this works only if your integer id values are cast into type jsonb as well. Since a type int is not directly castable into type jsonb, your need the intermediate step via type text, which yield the strange syntax id::text::jsonb:

SELECT
    *
FROM a
JOIN b ON b.ids @> a.id::text::jsonb

If your column is of type json instead jsonb, you need to cast it into type jsonb because otherwise the operator would not work:

b.ids::jsonb @> ...
Sign up to request clarification or add additional context in comments.

2 Comments

here ids is json datatype, so when I using this query it shows ANY requires array on right side
is it possible to replace array values with related table values

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.