In a postgres 10 database, there are three tables:
main:
id | name
-----------
1 | first
2 | second
3 | third
…
substances
id | name
----------------------
1 | gold
2 | silver
3 | aluminum
…
link
id | id_main | id_substance
---------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 1
5 | 3 | 2
6 | 3 | 3
How to make query to return a json object like this?
[
{
"name": "first",
"substances": ["gold", "silver", "aluminum"]
},
{
"name": "second",
"substances": ["gold"]
},
{
"name": "third",
"substances": ["silver", "aluminum"]
}
]