Grabbing array-like data via SQL joins to form complete object
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 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 |
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 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 |
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 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 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 '18 at 21:08
N.J.Dawson
498419
498419
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 '18 at 20:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 20:51
add a comment |
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 '18 at 20:51
add a comment |
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 '18 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 '18 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 '18 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 '18 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 '18 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