MySQL Stored Procedure to loop through 2 tables, select values, and store in 3rd












1















I have 3 tables users, memos, and memos_read.



users:



+---+----------+
|ID | NAME |
+---+----------+
| 1 | Mike |
| 4 | Susan |
| 5 | Peter |
| 6 | Mary |
+---+----------+


memos:



+---+----------+
|ID | CONTENT |
+---+----------+
| 15| Hello |
| 16| World |
| 17| Foo |
| 23| Bar |
+---+----------+


I want to create a stored procedure with a nested loop to create a "read map" for memos that have been read, creating something like:



memos_read:



+---------+----------+
| MEMO_ID | USER_ID |
+---------+----------+
| 15 | 1 |
| 15 | 4 |
| 15 | 5 |
| 15 | 6 |
| 16 | 1 |
| 16 | 4 |
| 16 | 5 |
| 16 | 6 |
| 17 | 1 |
| 17 | 4 |
| 17 | 5 |
| 17 | 6 |
| 23 | 1 |
| 23 | 4 |
| 23 | 5 |
| 23 | 6 |
+---------+----------+


Is this possible? Thank you in advanced!










share|improve this question


















  • 1





    That's just a cross join no sp required.

    – P.Salmon
    Nov 23 '18 at 6:36
















1















I have 3 tables users, memos, and memos_read.



users:



+---+----------+
|ID | NAME |
+---+----------+
| 1 | Mike |
| 4 | Susan |
| 5 | Peter |
| 6 | Mary |
+---+----------+


memos:



+---+----------+
|ID | CONTENT |
+---+----------+
| 15| Hello |
| 16| World |
| 17| Foo |
| 23| Bar |
+---+----------+


I want to create a stored procedure with a nested loop to create a "read map" for memos that have been read, creating something like:



memos_read:



+---------+----------+
| MEMO_ID | USER_ID |
+---------+----------+
| 15 | 1 |
| 15 | 4 |
| 15 | 5 |
| 15 | 6 |
| 16 | 1 |
| 16 | 4 |
| 16 | 5 |
| 16 | 6 |
| 17 | 1 |
| 17 | 4 |
| 17 | 5 |
| 17 | 6 |
| 23 | 1 |
| 23 | 4 |
| 23 | 5 |
| 23 | 6 |
+---------+----------+


Is this possible? Thank you in advanced!










share|improve this question


















  • 1





    That's just a cross join no sp required.

    – P.Salmon
    Nov 23 '18 at 6:36














1












1








1








I have 3 tables users, memos, and memos_read.



users:



+---+----------+
|ID | NAME |
+---+----------+
| 1 | Mike |
| 4 | Susan |
| 5 | Peter |
| 6 | Mary |
+---+----------+


memos:



+---+----------+
|ID | CONTENT |
+---+----------+
| 15| Hello |
| 16| World |
| 17| Foo |
| 23| Bar |
+---+----------+


I want to create a stored procedure with a nested loop to create a "read map" for memos that have been read, creating something like:



memos_read:



+---------+----------+
| MEMO_ID | USER_ID |
+---------+----------+
| 15 | 1 |
| 15 | 4 |
| 15 | 5 |
| 15 | 6 |
| 16 | 1 |
| 16 | 4 |
| 16 | 5 |
| 16 | 6 |
| 17 | 1 |
| 17 | 4 |
| 17 | 5 |
| 17 | 6 |
| 23 | 1 |
| 23 | 4 |
| 23 | 5 |
| 23 | 6 |
+---------+----------+


Is this possible? Thank you in advanced!










share|improve this question














I have 3 tables users, memos, and memos_read.



users:



+---+----------+
|ID | NAME |
+---+----------+
| 1 | Mike |
| 4 | Susan |
| 5 | Peter |
| 6 | Mary |
+---+----------+


memos:



+---+----------+
|ID | CONTENT |
+---+----------+
| 15| Hello |
| 16| World |
| 17| Foo |
| 23| Bar |
+---+----------+


I want to create a stored procedure with a nested loop to create a "read map" for memos that have been read, creating something like:



memos_read:



+---------+----------+
| MEMO_ID | USER_ID |
+---------+----------+
| 15 | 1 |
| 15 | 4 |
| 15 | 5 |
| 15 | 6 |
| 16 | 1 |
| 16 | 4 |
| 16 | 5 |
| 16 | 6 |
| 17 | 1 |
| 17 | 4 |
| 17 | 5 |
| 17 | 6 |
| 23 | 1 |
| 23 | 4 |
| 23 | 5 |
| 23 | 6 |
+---------+----------+


Is this possible? Thank you in advanced!







mysql stored-procedures






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 6:21









Mike FengMike Feng

3891314




3891314








  • 1





    That's just a cross join no sp required.

    – P.Salmon
    Nov 23 '18 at 6:36














  • 1





    That's just a cross join no sp required.

    – P.Salmon
    Nov 23 '18 at 6:36








1




1





That's just a cross join no sp required.

– P.Salmon
Nov 23 '18 at 6:36





That's just a cross join no sp required.

– P.Salmon
Nov 23 '18 at 6:36












2 Answers
2






active

oldest

votes


















1














@Nick's answer should work for you; however if you are not interested in creating a new table, and rather use the "read map" dynamically inside a query; you can utilize a Derived Table.



General query structure would be something as follows:



SELECT
read_map.MEMO_ID,
read_map.USER_ID,
...
FROM
(
SELECT
m.ID AS MEMO_ID,
u.ID AS USER_ID
FROM users AS u
CROSS JOIN memos AS m
) AS read_map

... -- here you can join with other tables as required
WHERE ...





share|improve this answer































    1














    You can use CREATE TABLE ... SELECT syntax to create the memos_read table:



    CREATE TABLE memos_read AS
    SELECT m.ID AS MEMO_ID, u.ID AS USER_ID
    FROM memos m
    CROSS JOIN users u


    Output is as you requested. Demo on dbfiddle






    share|improve this answer























      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%2f53441521%2fmysql-stored-procedure-to-loop-through-2-tables-select-values-and-store-in-3rd%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









      1














      @Nick's answer should work for you; however if you are not interested in creating a new table, and rather use the "read map" dynamically inside a query; you can utilize a Derived Table.



      General query structure would be something as follows:



      SELECT
      read_map.MEMO_ID,
      read_map.USER_ID,
      ...
      FROM
      (
      SELECT
      m.ID AS MEMO_ID,
      u.ID AS USER_ID
      FROM users AS u
      CROSS JOIN memos AS m
      ) AS read_map

      ... -- here you can join with other tables as required
      WHERE ...





      share|improve this answer




























        1














        @Nick's answer should work for you; however if you are not interested in creating a new table, and rather use the "read map" dynamically inside a query; you can utilize a Derived Table.



        General query structure would be something as follows:



        SELECT
        read_map.MEMO_ID,
        read_map.USER_ID,
        ...
        FROM
        (
        SELECT
        m.ID AS MEMO_ID,
        u.ID AS USER_ID
        FROM users AS u
        CROSS JOIN memos AS m
        ) AS read_map

        ... -- here you can join with other tables as required
        WHERE ...





        share|improve this answer


























          1












          1








          1







          @Nick's answer should work for you; however if you are not interested in creating a new table, and rather use the "read map" dynamically inside a query; you can utilize a Derived Table.



          General query structure would be something as follows:



          SELECT
          read_map.MEMO_ID,
          read_map.USER_ID,
          ...
          FROM
          (
          SELECT
          m.ID AS MEMO_ID,
          u.ID AS USER_ID
          FROM users AS u
          CROSS JOIN memos AS m
          ) AS read_map

          ... -- here you can join with other tables as required
          WHERE ...





          share|improve this answer













          @Nick's answer should work for you; however if you are not interested in creating a new table, and rather use the "read map" dynamically inside a query; you can utilize a Derived Table.



          General query structure would be something as follows:



          SELECT
          read_map.MEMO_ID,
          read_map.USER_ID,
          ...
          FROM
          (
          SELECT
          m.ID AS MEMO_ID,
          u.ID AS USER_ID
          FROM users AS u
          CROSS JOIN memos AS m
          ) AS read_map

          ... -- here you can join with other tables as required
          WHERE ...






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 7:25









          Madhur BhaiyaMadhur Bhaiya

          19.6k62236




          19.6k62236

























              1














              You can use CREATE TABLE ... SELECT syntax to create the memos_read table:



              CREATE TABLE memos_read AS
              SELECT m.ID AS MEMO_ID, u.ID AS USER_ID
              FROM memos m
              CROSS JOIN users u


              Output is as you requested. Demo on dbfiddle






              share|improve this answer




























                1














                You can use CREATE TABLE ... SELECT syntax to create the memos_read table:



                CREATE TABLE memos_read AS
                SELECT m.ID AS MEMO_ID, u.ID AS USER_ID
                FROM memos m
                CROSS JOIN users u


                Output is as you requested. Demo on dbfiddle






                share|improve this answer


























                  1












                  1








                  1







                  You can use CREATE TABLE ... SELECT syntax to create the memos_read table:



                  CREATE TABLE memos_read AS
                  SELECT m.ID AS MEMO_ID, u.ID AS USER_ID
                  FROM memos m
                  CROSS JOIN users u


                  Output is as you requested. Demo on dbfiddle






                  share|improve this answer













                  You can use CREATE TABLE ... SELECT syntax to create the memos_read table:



                  CREATE TABLE memos_read AS
                  SELECT m.ID AS MEMO_ID, u.ID AS USER_ID
                  FROM memos m
                  CROSS JOIN users u


                  Output is as you requested. Demo on dbfiddle







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 6:47









                  NickNick

                  36.6k132243




                  36.6k132243






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441521%2fmysql-stored-procedure-to-loop-through-2-tables-select-values-and-store-in-3rd%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

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      Origin of the phrase “under your belt”?