I have a table "store" and a table "product". Each store has multiple products but a product only has one store ( one to many relationship ). The tables might look something like:
store: id, name
product: id, name, store_id
when querying the store and its products using supabase I simply do:
.from("store")
.select(
id
name,
product(name)
)
which would return
id: "some_id",
name: "some_name",
products: [
{...},
{...}
]
}
or something along those lines depending on what data I want. However, I need to run this query in pure SQL and I can’t seem to figure it out.
I tried to JOIN the tables together but it leads to a lot of duplicated data since the store’s data is in all the rows
2
Answers
This is not an Answer.
In MySql you can use query like that :
If you inserted data like :
you will get :
Notes:
AND (s.id=1)
or modify it.In PostgreSQL you use ARRAY_TO_STRING() and ARRAY_AGG() functions instead of GROUP_CONCAT() in MySql. I don’t have PostgreSQL to test.
Prior to version 8.4, you would have had to define the function ARRAY_AGG before using it:
I found this on this Site
This will generate one JSON value per store:
Online example