MySQL update table based on another tables value










49















I have a two tables,



Here is my first table,



ID SUBST_ID CREATED_ID
1 031938 TEST123
2 930111 COOL123
3 000391 THIS109
4 039301 BRO1011
5 123456 COOL938
... ... ...


This is my second table,



ID SERIAL_ID BRANCH_ID
1 039301 NULL
2 000391 NULL
3 123456 NULL
... ... ...


I need to some how update all rows within my second table using data from my first table.



It would need to do this all in one update query.



Both SUBST_ID and SERIAL_ID match, it needs to grab the created_id from the first table and insert it into the second table.



So the second table would become the following,



ID SERIAL_ID BRANCH_ID
1 039301 BRO1011
2 000391 THIS109
3 123456 COOL938
... ... ...


Thank you for your help and guidance.










share|improve this question


























    49















    I have a two tables,



    Here is my first table,



    ID SUBST_ID CREATED_ID
    1 031938 TEST123
    2 930111 COOL123
    3 000391 THIS109
    4 039301 BRO1011
    5 123456 COOL938
    ... ... ...


    This is my second table,



    ID SERIAL_ID BRANCH_ID
    1 039301 NULL
    2 000391 NULL
    3 123456 NULL
    ... ... ...


    I need to some how update all rows within my second table using data from my first table.



    It would need to do this all in one update query.



    Both SUBST_ID and SERIAL_ID match, it needs to grab the created_id from the first table and insert it into the second table.



    So the second table would become the following,



    ID SERIAL_ID BRANCH_ID
    1 039301 BRO1011
    2 000391 THIS109
    3 123456 COOL938
    ... ... ...


    Thank you for your help and guidance.










    share|improve this question
























      49












      49








      49


      14






      I have a two tables,



      Here is my first table,



      ID SUBST_ID CREATED_ID
      1 031938 TEST123
      2 930111 COOL123
      3 000391 THIS109
      4 039301 BRO1011
      5 123456 COOL938
      ... ... ...


      This is my second table,



      ID SERIAL_ID BRANCH_ID
      1 039301 NULL
      2 000391 NULL
      3 123456 NULL
      ... ... ...


      I need to some how update all rows within my second table using data from my first table.



      It would need to do this all in one update query.



      Both SUBST_ID and SERIAL_ID match, it needs to grab the created_id from the first table and insert it into the second table.



      So the second table would become the following,



      ID SERIAL_ID BRANCH_ID
      1 039301 BRO1011
      2 000391 THIS109
      3 123456 COOL938
      ... ... ...


      Thank you for your help and guidance.










      share|improve this question














      I have a two tables,



      Here is my first table,



      ID SUBST_ID CREATED_ID
      1 031938 TEST123
      2 930111 COOL123
      3 000391 THIS109
      4 039301 BRO1011
      5 123456 COOL938
      ... ... ...


      This is my second table,



      ID SERIAL_ID BRANCH_ID
      1 039301 NULL
      2 000391 NULL
      3 123456 NULL
      ... ... ...


      I need to some how update all rows within my second table using data from my first table.



      It would need to do this all in one update query.



      Both SUBST_ID and SERIAL_ID match, it needs to grab the created_id from the first table and insert it into the second table.



      So the second table would become the following,



      ID SERIAL_ID BRANCH_ID
      1 039301 BRO1011
      2 000391 THIS109
      3 123456 COOL938
      ... ... ...


      Thank you for your help and guidance.







      mysql join sql-update






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 12 '12 at 18:52









      verheesjverheesj

      73321022




      73321022






















          6 Answers
          6






          active

          oldest

          votes


















          105














          UPDATE TABLE2
          JOIN TABLE1
          ON TABLE2.SERIAL_ID = TABLE1.SUBST_ID
          SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;





          share|improve this answer
































            15














            In addition to Tom's answer if you need to repeat the operation frequently and want to save time you can do:



            UPDATE TABLE1
            JOIN TABLE2
            ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
            SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID
            WHERE TABLE2.BRANCH_ID IS NULL





            share|improve this answer






























              3














              I think this should work



              UPDATE secondTable
              JOIN firsTable ON secondTable.SERIAL_ID = firsTable.SUBST_ID
              SET BRANCH_ID = CREATED_ID





              share|improve this answer
































                2














                UPDATE TABLE2
                JOIN TABLE1
                ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID
                WHERE TABLE2.BRANCH_ID IS NULL or TABLE2.BRANCH_ID='';





                share|improve this answer

























                • Could you please elaborate more your answer adding a little more description about the solution you provide?

                  – abarisone
                  May 26 '15 at 6:29











                • by using join on subset_Id and Serial_Id you will get a Result set.and that result set will contain created id..now Concentrate on query after SET there we actually assigning or providing values from created_Id to Branch_Id ....hope you understand.. :)

                  – Mohammad Imran
                  Jul 24 '15 at 6:24



















                0














                Using INNER JOIN:



                UPDATE TABLE1
                INNER JOIN TABLE2 ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;


                Another alternative solution like below: Here I am using WHERE clause instead of JOIN



                UPDATE 
                TABLE1,
                TABLE2
                WHERE
                TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                SET
                TABLE2.BRANCH_ID = TABLE1.CREATED_ID;





                share|improve this answer
































                  0














                  You can use this too:



                  update TABLE1 set BRANCH_ID = ( select BRANCH_ID from TABLE2 where TABLE1.SUBST_ID = TABLE2.SERIAL_ID)


                  but with my experience I can say that this way is so slow and not recommend it!






                  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%2f12394506%2fmysql-update-table-based-on-another-tables-value%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown

























                    6 Answers
                    6






                    active

                    oldest

                    votes








                    6 Answers
                    6






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    105














                    UPDATE TABLE2
                    JOIN TABLE1
                    ON TABLE2.SERIAL_ID = TABLE1.SUBST_ID
                    SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;





                    share|improve this answer





























                      105














                      UPDATE TABLE2
                      JOIN TABLE1
                      ON TABLE2.SERIAL_ID = TABLE1.SUBST_ID
                      SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;





                      share|improve this answer



























                        105












                        105








                        105







                        UPDATE TABLE2
                        JOIN TABLE1
                        ON TABLE2.SERIAL_ID = TABLE1.SUBST_ID
                        SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;





                        share|improve this answer















                        UPDATE TABLE2
                        JOIN TABLE1
                        ON TABLE2.SERIAL_ID = TABLE1.SUBST_ID
                        SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Mar 26 '17 at 14:27

























                        answered Sep 12 '12 at 18:58









                        TomTom

                        5,40031340




                        5,40031340























                            15














                            In addition to Tom's answer if you need to repeat the operation frequently and want to save time you can do:



                            UPDATE TABLE1
                            JOIN TABLE2
                            ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                            SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID
                            WHERE TABLE2.BRANCH_ID IS NULL





                            share|improve this answer



























                              15














                              In addition to Tom's answer if you need to repeat the operation frequently and want to save time you can do:



                              UPDATE TABLE1
                              JOIN TABLE2
                              ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                              SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID
                              WHERE TABLE2.BRANCH_ID IS NULL





                              share|improve this answer

























                                15












                                15








                                15







                                In addition to Tom's answer if you need to repeat the operation frequently and want to save time you can do:



                                UPDATE TABLE1
                                JOIN TABLE2
                                ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID
                                WHERE TABLE2.BRANCH_ID IS NULL





                                share|improve this answer













                                In addition to Tom's answer if you need to repeat the operation frequently and want to save time you can do:



                                UPDATE TABLE1
                                JOIN TABLE2
                                ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID
                                WHERE TABLE2.BRANCH_ID IS NULL






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Oct 13 '13 at 16:17









                                RafaSashiRafaSashi

                                10.2k65672




                                10.2k65672





















                                    3














                                    I think this should work



                                    UPDATE secondTable
                                    JOIN firsTable ON secondTable.SERIAL_ID = firsTable.SUBST_ID
                                    SET BRANCH_ID = CREATED_ID





                                    share|improve this answer





























                                      3














                                      I think this should work



                                      UPDATE secondTable
                                      JOIN firsTable ON secondTable.SERIAL_ID = firsTable.SUBST_ID
                                      SET BRANCH_ID = CREATED_ID





                                      share|improve this answer



























                                        3












                                        3








                                        3







                                        I think this should work



                                        UPDATE secondTable
                                        JOIN firsTable ON secondTable.SERIAL_ID = firsTable.SUBST_ID
                                        SET BRANCH_ID = CREATED_ID





                                        share|improve this answer















                                        I think this should work



                                        UPDATE secondTable
                                        JOIN firsTable ON secondTable.SERIAL_ID = firsTable.SUBST_ID
                                        SET BRANCH_ID = CREATED_ID






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Jan 2 '17 at 11:29









                                        Faisal

                                        2,65622233




                                        2,65622233










                                        answered Sep 12 '12 at 18:57









                                        Gonzalo.-Gonzalo.-

                                        8,41633968




                                        8,41633968





















                                            2














                                            UPDATE TABLE2
                                            JOIN TABLE1
                                            ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                            SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID
                                            WHERE TABLE2.BRANCH_ID IS NULL or TABLE2.BRANCH_ID='';





                                            share|improve this answer

























                                            • Could you please elaborate more your answer adding a little more description about the solution you provide?

                                              – abarisone
                                              May 26 '15 at 6:29











                                            • by using join on subset_Id and Serial_Id you will get a Result set.and that result set will contain created id..now Concentrate on query after SET there we actually assigning or providing values from created_Id to Branch_Id ....hope you understand.. :)

                                              – Mohammad Imran
                                              Jul 24 '15 at 6:24
















                                            2














                                            UPDATE TABLE2
                                            JOIN TABLE1
                                            ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                            SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID
                                            WHERE TABLE2.BRANCH_ID IS NULL or TABLE2.BRANCH_ID='';





                                            share|improve this answer

























                                            • Could you please elaborate more your answer adding a little more description about the solution you provide?

                                              – abarisone
                                              May 26 '15 at 6:29











                                            • by using join on subset_Id and Serial_Id you will get a Result set.and that result set will contain created id..now Concentrate on query after SET there we actually assigning or providing values from created_Id to Branch_Id ....hope you understand.. :)

                                              – Mohammad Imran
                                              Jul 24 '15 at 6:24














                                            2












                                            2








                                            2







                                            UPDATE TABLE2
                                            JOIN TABLE1
                                            ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                            SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID
                                            WHERE TABLE2.BRANCH_ID IS NULL or TABLE2.BRANCH_ID='';





                                            share|improve this answer















                                            UPDATE TABLE2
                                            JOIN TABLE1
                                            ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                            SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID
                                            WHERE TABLE2.BRANCH_ID IS NULL or TABLE2.BRANCH_ID='';






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Oct 8 '15 at 9:00









                                            Pang

                                            6,9291664102




                                            6,9291664102










                                            answered May 26 '15 at 6:25









                                            Mohammad ImranMohammad Imran

                                            656




                                            656












                                            • Could you please elaborate more your answer adding a little more description about the solution you provide?

                                              – abarisone
                                              May 26 '15 at 6:29











                                            • by using join on subset_Id and Serial_Id you will get a Result set.and that result set will contain created id..now Concentrate on query after SET there we actually assigning or providing values from created_Id to Branch_Id ....hope you understand.. :)

                                              – Mohammad Imran
                                              Jul 24 '15 at 6:24


















                                            • Could you please elaborate more your answer adding a little more description about the solution you provide?

                                              – abarisone
                                              May 26 '15 at 6:29











                                            • by using join on subset_Id and Serial_Id you will get a Result set.and that result set will contain created id..now Concentrate on query after SET there we actually assigning or providing values from created_Id to Branch_Id ....hope you understand.. :)

                                              – Mohammad Imran
                                              Jul 24 '15 at 6:24

















                                            Could you please elaborate more your answer adding a little more description about the solution you provide?

                                            – abarisone
                                            May 26 '15 at 6:29





                                            Could you please elaborate more your answer adding a little more description about the solution you provide?

                                            – abarisone
                                            May 26 '15 at 6:29













                                            by using join on subset_Id and Serial_Id you will get a Result set.and that result set will contain created id..now Concentrate on query after SET there we actually assigning or providing values from created_Id to Branch_Id ....hope you understand.. :)

                                            – Mohammad Imran
                                            Jul 24 '15 at 6:24






                                            by using join on subset_Id and Serial_Id you will get a Result set.and that result set will contain created id..now Concentrate on query after SET there we actually assigning or providing values from created_Id to Branch_Id ....hope you understand.. :)

                                            – Mohammad Imran
                                            Jul 24 '15 at 6:24












                                            0














                                            Using INNER JOIN:



                                            UPDATE TABLE1
                                            INNER JOIN TABLE2 ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                            SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;


                                            Another alternative solution like below: Here I am using WHERE clause instead of JOIN



                                            UPDATE 
                                            TABLE1,
                                            TABLE2
                                            WHERE
                                            TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                            SET
                                            TABLE2.BRANCH_ID = TABLE1.CREATED_ID;





                                            share|improve this answer





























                                              0














                                              Using INNER JOIN:



                                              UPDATE TABLE1
                                              INNER JOIN TABLE2 ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                              SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;


                                              Another alternative solution like below: Here I am using WHERE clause instead of JOIN



                                              UPDATE 
                                              TABLE1,
                                              TABLE2
                                              WHERE
                                              TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                              SET
                                              TABLE2.BRANCH_ID = TABLE1.CREATED_ID;





                                              share|improve this answer



























                                                0












                                                0








                                                0







                                                Using INNER JOIN:



                                                UPDATE TABLE1
                                                INNER JOIN TABLE2 ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                                SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;


                                                Another alternative solution like below: Here I am using WHERE clause instead of JOIN



                                                UPDATE 
                                                TABLE1,
                                                TABLE2
                                                WHERE
                                                TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                                SET
                                                TABLE2.BRANCH_ID = TABLE1.CREATED_ID;





                                                share|improve this answer















                                                Using INNER JOIN:



                                                UPDATE TABLE1
                                                INNER JOIN TABLE2 ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                                SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;


                                                Another alternative solution like below: Here I am using WHERE clause instead of JOIN



                                                UPDATE 
                                                TABLE1,
                                                TABLE2
                                                WHERE
                                                TABLE1.SUBST_ID = TABLE2.SERIAL_ID
                                                SET
                                                TABLE2.BRANCH_ID = TABLE1.CREATED_ID;






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Jan 2 '17 at 11:08

























                                                answered Jan 2 '17 at 10:59









                                                FaisalFaisal

                                                2,65622233




                                                2,65622233





















                                                    0














                                                    You can use this too:



                                                    update TABLE1 set BRANCH_ID = ( select BRANCH_ID from TABLE2 where TABLE1.SUBST_ID = TABLE2.SERIAL_ID)


                                                    but with my experience I can say that this way is so slow and not recommend it!






                                                    share|improve this answer



























                                                      0














                                                      You can use this too:



                                                      update TABLE1 set BRANCH_ID = ( select BRANCH_ID from TABLE2 where TABLE1.SUBST_ID = TABLE2.SERIAL_ID)


                                                      but with my experience I can say that this way is so slow and not recommend it!






                                                      share|improve this answer

























                                                        0












                                                        0








                                                        0







                                                        You can use this too:



                                                        update TABLE1 set BRANCH_ID = ( select BRANCH_ID from TABLE2 where TABLE1.SUBST_ID = TABLE2.SERIAL_ID)


                                                        but with my experience I can say that this way is so slow and not recommend it!






                                                        share|improve this answer













                                                        You can use this too:



                                                        update TABLE1 set BRANCH_ID = ( select BRANCH_ID from TABLE2 where TABLE1.SUBST_ID = TABLE2.SERIAL_ID)


                                                        but with my experience I can say that this way is so slow and not recommend it!







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Nov 30 '17 at 14:49









                                                        AbadisAbadis

                                                        1,14131533




                                                        1,14131533



























                                                            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%2f12394506%2fmysql-update-table-based-on-another-tables-value%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

                                                            Use pre created SQLite database for Android project in kotlin

                                                            Darth Vader #20

                                                            Ondo