Grabbing array-like data via SQL joins to form complete object










0














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?










share|improve this question


























    0














    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?










    share|improve this question
























      0












      0








      0







      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?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 '18 at 21:08









      N.J.Dawson

      498419




      498419






















          1 Answer
          1






          active

          oldest

          votes


















          1














          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?).






          share|improve this answer




















          • 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










          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
          );



          );













          draft saved

          draft discarded


















          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









          1














          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?).






          share|improve this answer




















          • 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















          1














          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?).






          share|improve this answer




















          • 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













          1












          1








          1






          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?).






          share|improve this answer












          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?).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20