I'm wondering if it's possible to do a left outer join between a json_array_elements of a table column and another table? Something like the following, but this doesn't work.
SELECT *
FROM foo,
json_array_elements (foo.bars :: json) foo_bars
LEFT OUTER JOIN bar ON (foo_bars ->> 'id') :: BIGINT = bar.ID;
The table structure is like the following
FOO
------------------------------------------
| ID | NAME | BARS |
|------------------------------------------|
| 1 | FOO1 | [{ "id" : 1}, { "id" : 2 }]|
|------------------------------------------|
| 2 | FOO1 | [] |
------------------------------------------
BAR
-------------
| ID | NAME |
|-------------|
| 1 | BAR1 |
|-------------|
| 2 | BAR2 |
-------------
I would expect the output of the query to be
--------------------------------------------------------
| ID | NAME | BARS | ID | NAME |
|------------------------------------------|-------------|
| 1 | FOO1 | [{ "id" : 1}, { "id" : 2 }]| 1 | BAR1 |
|------------------------------------------|-------------|
| 1 | FOO1 | [{ "id" : 1}, { "id" : 2 }]| 2 | BAR2 |
|------------------------------------------|-------------|
| 2 | FOO1 | [] | null | null |
--------------------------------------------------------
To answer your question: Yes it is possible and your query does exactly that.
We can prove it by introducing a third row in foo
table:
http://sqlfiddle.com/#!15/06dfe/2
Your problem is not with LEFT JOIN
to json_array_elements
but with implicit lateral cross join. Your query is equivalent to:
SELECT *
FROM foo
CROSS JOIN LATERAL json_array_elements (foo.bars :: json) foo_bars
LEFT OUTER JOIN bar ON (foo_bars ->> 'id') :: BIGINT = bar.ID;
http://sqlfiddle.com/#!15/06dfe/5
What you want is a lateral left join between foo
and json_array_elements
:
SELECT *
FROM foo LEFT JOIN LATERAL
json_array_elements (foo.bars :: json) foo_bars ON true
LEFT OUTER JOIN bar ON (foo_bars ->> 'id') :: BIGINT = bar.ID;
http://sqlfiddle.com/#!15/06dfe/6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With