Combining Data in Two Tables SQL











up vote
0
down vote

favorite












I'm sure a very basic question, but I'm continue to be stuck:



Table A - image_number, camera_type, total_sales
Table B - image_number, keyword


Table A has one ROW for each image_number - example:



image_number="AXJ789, camera_type="Nikon", total_sales=678
image_number="JIJ123", camera_type="Canon", total_sales=999
image_number="KNI908", camera_type="Sony", total_sales=565


Table B has many ROWs for each image_number - example:



image_number="AXJ789", keyword = "rain"
image_number="AXJ789", keyword = "mountain"
image_number="AXJ789", keyword = "grass"
image_number="AXJ789", keyword = "cloud"


What I'm trying to do is JOIN the two tables so that I can generate the following output:




image_number="AXJ789", camera_type=678, camera_type="Nikon", keyword(1) = "rain", keyword(2) = "mountain", keyword(3) = "grass", keyword(4) = "cloud"




In other words, I want to have all items in each ROW in table A + all the items from table B. For each image_number in Table A, there could be no "keywords" in Table B or 50 keywords - depends on the image.



When I do an INNER JOIN, of course I can get one "keyword" from table B, but I can't figure out how to get all of them.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm sure a very basic question, but I'm continue to be stuck:



    Table A - image_number, camera_type, total_sales
    Table B - image_number, keyword


    Table A has one ROW for each image_number - example:



    image_number="AXJ789, camera_type="Nikon", total_sales=678
    image_number="JIJ123", camera_type="Canon", total_sales=999
    image_number="KNI908", camera_type="Sony", total_sales=565


    Table B has many ROWs for each image_number - example:



    image_number="AXJ789", keyword = "rain"
    image_number="AXJ789", keyword = "mountain"
    image_number="AXJ789", keyword = "grass"
    image_number="AXJ789", keyword = "cloud"


    What I'm trying to do is JOIN the two tables so that I can generate the following output:




    image_number="AXJ789", camera_type=678, camera_type="Nikon", keyword(1) = "rain", keyword(2) = "mountain", keyword(3) = "grass", keyword(4) = "cloud"




    In other words, I want to have all items in each ROW in table A + all the items from table B. For each image_number in Table A, there could be no "keywords" in Table B or 50 keywords - depends on the image.



    When I do an INNER JOIN, of course I can get one "keyword" from table B, but I can't figure out how to get all of them.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm sure a very basic question, but I'm continue to be stuck:



      Table A - image_number, camera_type, total_sales
      Table B - image_number, keyword


      Table A has one ROW for each image_number - example:



      image_number="AXJ789, camera_type="Nikon", total_sales=678
      image_number="JIJ123", camera_type="Canon", total_sales=999
      image_number="KNI908", camera_type="Sony", total_sales=565


      Table B has many ROWs for each image_number - example:



      image_number="AXJ789", keyword = "rain"
      image_number="AXJ789", keyword = "mountain"
      image_number="AXJ789", keyword = "grass"
      image_number="AXJ789", keyword = "cloud"


      What I'm trying to do is JOIN the two tables so that I can generate the following output:




      image_number="AXJ789", camera_type=678, camera_type="Nikon", keyword(1) = "rain", keyword(2) = "mountain", keyword(3) = "grass", keyword(4) = "cloud"




      In other words, I want to have all items in each ROW in table A + all the items from table B. For each image_number in Table A, there could be no "keywords" in Table B or 50 keywords - depends on the image.



      When I do an INNER JOIN, of course I can get one "keyword" from table B, but I can't figure out how to get all of them.










      share|improve this question















      I'm sure a very basic question, but I'm continue to be stuck:



      Table A - image_number, camera_type, total_sales
      Table B - image_number, keyword


      Table A has one ROW for each image_number - example:



      image_number="AXJ789, camera_type="Nikon", total_sales=678
      image_number="JIJ123", camera_type="Canon", total_sales=999
      image_number="KNI908", camera_type="Sony", total_sales=565


      Table B has many ROWs for each image_number - example:



      image_number="AXJ789", keyword = "rain"
      image_number="AXJ789", keyword = "mountain"
      image_number="AXJ789", keyword = "grass"
      image_number="AXJ789", keyword = "cloud"


      What I'm trying to do is JOIN the two tables so that I can generate the following output:




      image_number="AXJ789", camera_type=678, camera_type="Nikon", keyword(1) = "rain", keyword(2) = "mountain", keyword(3) = "grass", keyword(4) = "cloud"




      In other words, I want to have all items in each ROW in table A + all the items from table B. For each image_number in Table A, there could be no "keywords" in Table B or 50 keywords - depends on the image.



      When I do an INNER JOIN, of course I can get one "keyword" from table B, but I can't figure out how to get all of them.







      mysql sql join






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 2:28









      kit

      986316




      986316










      asked Nov 19 at 0:39









      Rick Leckrone

      54




      54
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can generate a comma-separated list of keywords for each image using GROUP_CONCAT and JOIN (but use a LEFT JOIN if an image may have no keywords).



          SELECT a.*, GROUP_CONCAT(b.keyword) AS keyword_list
          FROM a
          JOIN b on b.image_number = a.image_number
          GROUP BY a.image_number


          Output for your sample data:



          image_number    camera_type     total_sales     keyword_list
          AXJ789 Nikon 678 rain,mountain,grass,cloud


          Demo on dbfiddle



          You can then parse this into an array in your application, for example in PHP (if you have read the row into $row):



          $keywords = explode(',', $row['keyword_list']);
          print_r($keywords);


          Output:



          Array
          (
          [0] => rain
          [1] => mountain
          [2] => grass
          [3] => cloud
          )





          share|improve this answer























          • Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
            – Rick Leckrone
            Nov 20 at 0:34










          • Worked perfectly!
            – Rick Leckrone
            Nov 20 at 1:20










          • That's great. Glad I could help.
            – Nick
            Nov 20 at 1:53


















          up vote
          1
          down vote













          You can concatenate the keywords together:



          select a.*,
          (select group_concat(b.keyword)
          from b
          where b.image_number = a. image_number
          ) as keywords
          from a;


          This creates a comma-delimited list of the keywords. This is much simpler (in MySQL) than trying to put them in separate columns. In fact, if you wanted separate columns, I might suggest parsing this result:



          select a.*,  -- or whatever columns you want
          substring_index(keywords, ',' 1) as keyword1,
          substring_index(substring_index(keywords, ',' 2), ',', -1) as keyword2,
          substring_index(substring_index(keywords, ',' 3), ',', -1) as keyword3,
          substring_index(substring_index(keywords, ',' 4), ',', -1) as keyword4
          from a left join
          (select b.image_number, group_concat(b.keyword) as keywords
          from b
          group by b.image_number
          ) b
          on b.image_number = a. image_number;





          share|improve this answer





















          • Thank you so much. Will give it a spin.
            – Rick Leckrone
            Nov 20 at 0:32











          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',
          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%2f53366927%2fcombining-data-in-two-tables-sql%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          You can generate a comma-separated list of keywords for each image using GROUP_CONCAT and JOIN (but use a LEFT JOIN if an image may have no keywords).



          SELECT a.*, GROUP_CONCAT(b.keyword) AS keyword_list
          FROM a
          JOIN b on b.image_number = a.image_number
          GROUP BY a.image_number


          Output for your sample data:



          image_number    camera_type     total_sales     keyword_list
          AXJ789 Nikon 678 rain,mountain,grass,cloud


          Demo on dbfiddle



          You can then parse this into an array in your application, for example in PHP (if you have read the row into $row):



          $keywords = explode(',', $row['keyword_list']);
          print_r($keywords);


          Output:



          Array
          (
          [0] => rain
          [1] => mountain
          [2] => grass
          [3] => cloud
          )





          share|improve this answer























          • Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
            – Rick Leckrone
            Nov 20 at 0:34










          • Worked perfectly!
            – Rick Leckrone
            Nov 20 at 1:20










          • That's great. Glad I could help.
            – Nick
            Nov 20 at 1:53















          up vote
          0
          down vote



          accepted










          You can generate a comma-separated list of keywords for each image using GROUP_CONCAT and JOIN (but use a LEFT JOIN if an image may have no keywords).



          SELECT a.*, GROUP_CONCAT(b.keyword) AS keyword_list
          FROM a
          JOIN b on b.image_number = a.image_number
          GROUP BY a.image_number


          Output for your sample data:



          image_number    camera_type     total_sales     keyword_list
          AXJ789 Nikon 678 rain,mountain,grass,cloud


          Demo on dbfiddle



          You can then parse this into an array in your application, for example in PHP (if you have read the row into $row):



          $keywords = explode(',', $row['keyword_list']);
          print_r($keywords);


          Output:



          Array
          (
          [0] => rain
          [1] => mountain
          [2] => grass
          [3] => cloud
          )





          share|improve this answer























          • Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
            – Rick Leckrone
            Nov 20 at 0:34










          • Worked perfectly!
            – Rick Leckrone
            Nov 20 at 1:20










          • That's great. Glad I could help.
            – Nick
            Nov 20 at 1:53













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can generate a comma-separated list of keywords for each image using GROUP_CONCAT and JOIN (but use a LEFT JOIN if an image may have no keywords).



          SELECT a.*, GROUP_CONCAT(b.keyword) AS keyword_list
          FROM a
          JOIN b on b.image_number = a.image_number
          GROUP BY a.image_number


          Output for your sample data:



          image_number    camera_type     total_sales     keyword_list
          AXJ789 Nikon 678 rain,mountain,grass,cloud


          Demo on dbfiddle



          You can then parse this into an array in your application, for example in PHP (if you have read the row into $row):



          $keywords = explode(',', $row['keyword_list']);
          print_r($keywords);


          Output:



          Array
          (
          [0] => rain
          [1] => mountain
          [2] => grass
          [3] => cloud
          )





          share|improve this answer














          You can generate a comma-separated list of keywords for each image using GROUP_CONCAT and JOIN (but use a LEFT JOIN if an image may have no keywords).



          SELECT a.*, GROUP_CONCAT(b.keyword) AS keyword_list
          FROM a
          JOIN b on b.image_number = a.image_number
          GROUP BY a.image_number


          Output for your sample data:



          image_number    camera_type     total_sales     keyword_list
          AXJ789 Nikon 678 rain,mountain,grass,cloud


          Demo on dbfiddle



          You can then parse this into an array in your application, for example in PHP (if you have read the row into $row):



          $keywords = explode(',', $row['keyword_list']);
          print_r($keywords);


          Output:



          Array
          (
          [0] => rain
          [1] => mountain
          [2] => grass
          [3] => cloud
          )






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 1:17

























          answered Nov 19 at 1:00









          Nick

          20.1k51434




          20.1k51434












          • Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
            – Rick Leckrone
            Nov 20 at 0:34










          • Worked perfectly!
            – Rick Leckrone
            Nov 20 at 1:20










          • That's great. Glad I could help.
            – Nick
            Nov 20 at 1:53


















          • Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
            – Rick Leckrone
            Nov 20 at 0:34










          • Worked perfectly!
            – Rick Leckrone
            Nov 20 at 1:20










          • That's great. Glad I could help.
            – Nick
            Nov 20 at 1:53
















          Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
          – Rick Leckrone
          Nov 20 at 0:34




          Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
          – Rick Leckrone
          Nov 20 at 0:34












          Worked perfectly!
          – Rick Leckrone
          Nov 20 at 1:20




          Worked perfectly!
          – Rick Leckrone
          Nov 20 at 1:20












          That's great. Glad I could help.
          – Nick
          Nov 20 at 1:53




          That's great. Glad I could help.
          – Nick
          Nov 20 at 1:53












          up vote
          1
          down vote













          You can concatenate the keywords together:



          select a.*,
          (select group_concat(b.keyword)
          from b
          where b.image_number = a. image_number
          ) as keywords
          from a;


          This creates a comma-delimited list of the keywords. This is much simpler (in MySQL) than trying to put them in separate columns. In fact, if you wanted separate columns, I might suggest parsing this result:



          select a.*,  -- or whatever columns you want
          substring_index(keywords, ',' 1) as keyword1,
          substring_index(substring_index(keywords, ',' 2), ',', -1) as keyword2,
          substring_index(substring_index(keywords, ',' 3), ',', -1) as keyword3,
          substring_index(substring_index(keywords, ',' 4), ',', -1) as keyword4
          from a left join
          (select b.image_number, group_concat(b.keyword) as keywords
          from b
          group by b.image_number
          ) b
          on b.image_number = a. image_number;





          share|improve this answer





















          • Thank you so much. Will give it a spin.
            – Rick Leckrone
            Nov 20 at 0:32















          up vote
          1
          down vote













          You can concatenate the keywords together:



          select a.*,
          (select group_concat(b.keyword)
          from b
          where b.image_number = a. image_number
          ) as keywords
          from a;


          This creates a comma-delimited list of the keywords. This is much simpler (in MySQL) than trying to put them in separate columns. In fact, if you wanted separate columns, I might suggest parsing this result:



          select a.*,  -- or whatever columns you want
          substring_index(keywords, ',' 1) as keyword1,
          substring_index(substring_index(keywords, ',' 2), ',', -1) as keyword2,
          substring_index(substring_index(keywords, ',' 3), ',', -1) as keyword3,
          substring_index(substring_index(keywords, ',' 4), ',', -1) as keyword4
          from a left join
          (select b.image_number, group_concat(b.keyword) as keywords
          from b
          group by b.image_number
          ) b
          on b.image_number = a. image_number;





          share|improve this answer





















          • Thank you so much. Will give it a spin.
            – Rick Leckrone
            Nov 20 at 0:32













          up vote
          1
          down vote










          up vote
          1
          down vote









          You can concatenate the keywords together:



          select a.*,
          (select group_concat(b.keyword)
          from b
          where b.image_number = a. image_number
          ) as keywords
          from a;


          This creates a comma-delimited list of the keywords. This is much simpler (in MySQL) than trying to put them in separate columns. In fact, if you wanted separate columns, I might suggest parsing this result:



          select a.*,  -- or whatever columns you want
          substring_index(keywords, ',' 1) as keyword1,
          substring_index(substring_index(keywords, ',' 2), ',', -1) as keyword2,
          substring_index(substring_index(keywords, ',' 3), ',', -1) as keyword3,
          substring_index(substring_index(keywords, ',' 4), ',', -1) as keyword4
          from a left join
          (select b.image_number, group_concat(b.keyword) as keywords
          from b
          group by b.image_number
          ) b
          on b.image_number = a. image_number;





          share|improve this answer












          You can concatenate the keywords together:



          select a.*,
          (select group_concat(b.keyword)
          from b
          where b.image_number = a. image_number
          ) as keywords
          from a;


          This creates a comma-delimited list of the keywords. This is much simpler (in MySQL) than trying to put them in separate columns. In fact, if you wanted separate columns, I might suggest parsing this result:



          select a.*,  -- or whatever columns you want
          substring_index(keywords, ',' 1) as keyword1,
          substring_index(substring_index(keywords, ',' 2), ',', -1) as keyword2,
          substring_index(substring_index(keywords, ',' 3), ',', -1) as keyword3,
          substring_index(substring_index(keywords, ',' 4), ',', -1) as keyword4
          from a left join
          (select b.image_number, group_concat(b.keyword) as keywords
          from b
          group by b.image_number
          ) b
          on b.image_number = a. image_number;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 0:43









          Gordon Linoff

          746k33285390




          746k33285390












          • Thank you so much. Will give it a spin.
            – Rick Leckrone
            Nov 20 at 0:32


















          • Thank you so much. Will give it a spin.
            – Rick Leckrone
            Nov 20 at 0:32
















          Thank you so much. Will give it a spin.
          – Rick Leckrone
          Nov 20 at 0:32




          Thank you so much. Will give it a spin.
          – Rick Leckrone
          Nov 20 at 0:32


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366927%2fcombining-data-in-two-tables-sql%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

          Ottavio Pratesi

          Tricia Helfer

          15 giugno