Skip to content
Advertisement

Left join add matched rows in child table to an array in parent row (as JSON format)

I have the following two tables:

My goal is to do a (left?) join and get all matching rows in the child table based on the FK in an array on the parent row and not inside the parent row itself on matching column names (see further down on desired output).

Examples of values in tables:

Parent table:

Child table:

What my desired output should look like:

Something like this query (with pseudo-ish SQL code):

Desired output (in JSON)

Advertisement

Answer

You can use nested FOR JSON clauses to achieve this.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement