Grabbing array-like data via SQL joins to form complete object
up vote
0
down vote
favorite
I have the following three tables:
drinks: id | name
ingredient: id | name
drink_ingredients: drinks.id | ingredients.id
When I create a "drink", I provide an array of ingredient ids as well. The relationship here is managed by the drink_ingredients table, which allows me to reuse ingredients.
I'm trying to create a query of which will return data representing the following:
drink { id: 0 | name: 'Coffee' | ingredients: [2, 3] }
Essentially meaning I would like to extract out the ingredient id attached to this drink and return them as an array.
I currently have
select * from "drinks" inner join "drink_ingredients" on "drinks"."id" = "drink_ingredients"."drink_id"
I'm still missing the step to retrieve the data from ingredients, but as well this, this returns:
{
"id": 0,
"owner": 18,
"name": "Coffee",
"drink_id": 0,
"ingredient_id": 2
},
{
"id": 0,
"owner": 18,
"name": "Coffee",
"drink_id": 0,
"ingredient_id": 3
},
Is it possible to correctly embed data this way in a single statement?
sql postgresql node-postgres
add a comment |
up vote
0
down vote
favorite
I have the following three tables:
drinks: id | name
ingredient: id | name
drink_ingredients: drinks.id | ingredients.id
When I create a "drink", I provide an array of ingredient ids as well. The relationship here is managed by the drink_ingredients table, which allows me to reuse ingredients.
I'm trying to create a query of which will return data representing the following:
drink { id: 0 | name: 'Coffee' | ingredients: [2, 3] }
Essentially meaning I would like to extract out the ingredient id attached to this drink and return them as an array.
I currently have
select * from "drinks" inner join "drink_ingredients" on "drinks"."id" = "drink_ingredients"."drink_id"
I'm still missing the step to retrieve the data from ingredients, but as well this, this returns:
{
"id": 0,
"owner": 18,
"name": "Coffee",
"drink_id": 0,
"ingredient_id": 2
},
{
"id": 0,
"owner": 18,
"name": "Coffee",
"drink_id": 0,
"ingredient_id": 3
},
Is it possible to correctly embed data this way in a single statement?
sql postgresql node-postgres
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following three tables:
drinks: id | name
ingredient: id | name
drink_ingredients: drinks.id | ingredients.id
When I create a "drink", I provide an array of ingredient ids as well. The relationship here is managed by the drink_ingredients table, which allows me to reuse ingredients.
I'm trying to create a query of which will return data representing the following:
drink { id: 0 | name: 'Coffee' | ingredients: [2, 3] }
Essentially meaning I would like to extract out the ingredient id attached to this drink and return them as an array.
I currently have
select * from "drinks" inner join "drink_ingredients" on "drinks"."id" = "drink_ingredients"."drink_id"
I'm still missing the step to retrieve the data from ingredients, but as well this, this returns:
{
"id": 0,
"owner": 18,
"name": "Coffee",
"drink_id": 0,
"ingredient_id": 2
},
{
"id": 0,
"owner": 18,
"name": "Coffee",
"drink_id": 0,
"ingredient_id": 3
},
Is it possible to correctly embed data this way in a single statement?
sql postgresql node-postgres
I have the following three tables:
drinks: id | name
ingredient: id | name
drink_ingredients: drinks.id | ingredients.id
When I create a "drink", I provide an array of ingredient ids as well. The relationship here is managed by the drink_ingredients table, which allows me to reuse ingredients.
I'm trying to create a query of which will return data representing the following:
drink { id: 0 | name: 'Coffee' | ingredients: [2, 3] }
Essentially meaning I would like to extract out the ingredient id attached to this drink and return them as an array.
I currently have
select * from "drinks" inner join "drink_ingredients" on "drinks"."id" = "drink_ingredients"."drink_id"
I'm still missing the step to retrieve the data from ingredients, but as well this, this returns:
{
"id": 0,
"owner": 18,
"name": "Coffee",
"drink_id": 0,
"ingredient_id": 2
},
{
"id": 0,
"owner": 18,
"name": "Coffee",
"drink_id": 0,
"ingredient_id": 3
},
Is it possible to correctly embed data this way in a single statement?
sql postgresql node-postgres
sql postgresql node-postgres
asked Nov 11 at 21:08
N.J.Dawson
488419
488419
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
This can be done using the array_agg function:
select d.id, d.name, array_agg(di.ingredient_id) as ingredients
from drinks d
left outer join drink_ingredients di on di.drink_id = d.id
group by d.id, d.name
As you (per your question) only want the array of ingredient-ids, you don't even need to join with ingredients. The left join is just in case you have a drink with no ingredients (water?).
I dreamed bigger and managed to get the JSON representation of my objects in their complete form rather than IDs. Your help on the join was what got me there, thanks very much for your assistance and concise answer!
– N.J.Dawson
Nov 12 at 20:51
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This can be done using the array_agg function:
select d.id, d.name, array_agg(di.ingredient_id) as ingredients
from drinks d
left outer join drink_ingredients di on di.drink_id = d.id
group by d.id, d.name
As you (per your question) only want the array of ingredient-ids, you don't even need to join with ingredients. The left join is just in case you have a drink with no ingredients (water?).
I dreamed bigger and managed to get the JSON representation of my objects in their complete form rather than IDs. Your help on the join was what got me there, thanks very much for your assistance and concise answer!
– N.J.Dawson
Nov 12 at 20:51
add a comment |
up vote
1
down vote
accepted
This can be done using the array_agg function:
select d.id, d.name, array_agg(di.ingredient_id) as ingredients
from drinks d
left outer join drink_ingredients di on di.drink_id = d.id
group by d.id, d.name
As you (per your question) only want the array of ingredient-ids, you don't even need to join with ingredients. The left join is just in case you have a drink with no ingredients (water?).
I dreamed bigger and managed to get the JSON representation of my objects in their complete form rather than IDs. Your help on the join was what got me there, thanks very much for your assistance and concise answer!
– N.J.Dawson
Nov 12 at 20:51
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This can be done using the array_agg function:
select d.id, d.name, array_agg(di.ingredient_id) as ingredients
from drinks d
left outer join drink_ingredients di on di.drink_id = d.id
group by d.id, d.name
As you (per your question) only want the array of ingredient-ids, you don't even need to join with ingredients. The left join is just in case you have a drink with no ingredients (water?).
This can be done using the array_agg function:
select d.id, d.name, array_agg(di.ingredient_id) as ingredients
from drinks d
left outer join drink_ingredients di on di.drink_id = d.id
group by d.id, d.name
As you (per your question) only want the array of ingredient-ids, you don't even need to join with ingredients. The left join is just in case you have a drink with no ingredients (water?).
answered Nov 11 at 21:44
Henning Koehler
1,129610
1,129610
I dreamed bigger and managed to get the JSON representation of my objects in their complete form rather than IDs. Your help on the join was what got me there, thanks very much for your assistance and concise answer!
– N.J.Dawson
Nov 12 at 20:51
add a comment |
I dreamed bigger and managed to get the JSON representation of my objects in their complete form rather than IDs. Your help on the join was what got me there, thanks very much for your assistance and concise answer!
– N.J.Dawson
Nov 12 at 20:51
I dreamed bigger and managed to get the JSON representation of my objects in their complete form rather than IDs. Your help on the join was what got me there, thanks very much for your assistance and concise answer!
– N.J.Dawson
Nov 12 at 20:51
I dreamed bigger and managed to get the JSON representation of my objects in their complete form rather than IDs. Your help on the join was what got me there, thanks very much for your assistance and concise answer!
– N.J.Dawson
Nov 12 at 20:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253249%2fgrabbing-array-like-data-via-sql-joins-to-form-complete-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown