Mysql: Updating column based on another columns value










1















I have about 300 records in my database that have invalid data in a column. The columns that are invalid reference a user_id instead of a id code (shown in table).



I'm trying to set the new_foreign_dealer_id = to the user_id inside the old_dealer_id_to_dealer column



So in the table, the user_id with the value 206 would search for 204 and replace 206's row column with 204's new_foreign_dealer_id



Users Table before:



+---------+-----------------------+-------------------------+
| user_id | new_foreign_dealer_id | old_dealer_id_to_delete |
+---------+-----------------------+-------------------------+
| 200 | 5 | 02-000012 |
| 204 | 8 | 02-000097 |
| 206 | 0 (invalid) | 204 (referneces user_id |
+---------+-----------------------+-------------------------+


Users Table after:



+---------+-----------------------+-------------------------+
| user_id | new_foreign_dealer_id | old_dealer_id_to_delete |
+---------+-----------------------+-------------------------+
| 200 | 5 | 02-000012 |
| 204 | 8 | 02-000097 |
| 206 | 8 | 204 (referneces user_id |
+---------+-----------------------+-------------------------+


This is the query I tried



UPDATE users SET dealer_id_foreign = dealer_id_foreign
WHERE dealer_id = user_id



NOTE: The column names in my query are correct. The ASCII columns are for clarity purposes.



edit: ended up using PHP to achieve this. still interested in a sql answer










share|improve this question



















  • 1





    I think Alex's answer give you a SQL solution. Let know if the query is still not resolved. Otherwise, Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

    – Madhur Bhaiya
    Nov 15 '18 at 20:23











  • what does it take for someone to upvote my question? :P

    – millman2394
    Nov 15 '18 at 21:38











  • Here you go. Upvoted :-)

    – Madhur Bhaiya
    Nov 16 '18 at 4:10











  • thank you!!! :)

    – millman2394
    Nov 16 '18 at 21:05















1















I have about 300 records in my database that have invalid data in a column. The columns that are invalid reference a user_id instead of a id code (shown in table).



I'm trying to set the new_foreign_dealer_id = to the user_id inside the old_dealer_id_to_dealer column



So in the table, the user_id with the value 206 would search for 204 and replace 206's row column with 204's new_foreign_dealer_id



Users Table before:



+---------+-----------------------+-------------------------+
| user_id | new_foreign_dealer_id | old_dealer_id_to_delete |
+---------+-----------------------+-------------------------+
| 200 | 5 | 02-000012 |
| 204 | 8 | 02-000097 |
| 206 | 0 (invalid) | 204 (referneces user_id |
+---------+-----------------------+-------------------------+


Users Table after:



+---------+-----------------------+-------------------------+
| user_id | new_foreign_dealer_id | old_dealer_id_to_delete |
+---------+-----------------------+-------------------------+
| 200 | 5 | 02-000012 |
| 204 | 8 | 02-000097 |
| 206 | 8 | 204 (referneces user_id |
+---------+-----------------------+-------------------------+


This is the query I tried



UPDATE users SET dealer_id_foreign = dealer_id_foreign
WHERE dealer_id = user_id



NOTE: The column names in my query are correct. The ASCII columns are for clarity purposes.



edit: ended up using PHP to achieve this. still interested in a sql answer










share|improve this question



















  • 1





    I think Alex's answer give you a SQL solution. Let know if the query is still not resolved. Otherwise, Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

    – Madhur Bhaiya
    Nov 15 '18 at 20:23











  • what does it take for someone to upvote my question? :P

    – millman2394
    Nov 15 '18 at 21:38











  • Here you go. Upvoted :-)

    – Madhur Bhaiya
    Nov 16 '18 at 4:10











  • thank you!!! :)

    – millman2394
    Nov 16 '18 at 21:05













1












1








1


0






I have about 300 records in my database that have invalid data in a column. The columns that are invalid reference a user_id instead of a id code (shown in table).



I'm trying to set the new_foreign_dealer_id = to the user_id inside the old_dealer_id_to_dealer column



So in the table, the user_id with the value 206 would search for 204 and replace 206's row column with 204's new_foreign_dealer_id



Users Table before:



+---------+-----------------------+-------------------------+
| user_id | new_foreign_dealer_id | old_dealer_id_to_delete |
+---------+-----------------------+-------------------------+
| 200 | 5 | 02-000012 |
| 204 | 8 | 02-000097 |
| 206 | 0 (invalid) | 204 (referneces user_id |
+---------+-----------------------+-------------------------+


Users Table after:



+---------+-----------------------+-------------------------+
| user_id | new_foreign_dealer_id | old_dealer_id_to_delete |
+---------+-----------------------+-------------------------+
| 200 | 5 | 02-000012 |
| 204 | 8 | 02-000097 |
| 206 | 8 | 204 (referneces user_id |
+---------+-----------------------+-------------------------+


This is the query I tried



UPDATE users SET dealer_id_foreign = dealer_id_foreign
WHERE dealer_id = user_id



NOTE: The column names in my query are correct. The ASCII columns are for clarity purposes.



edit: ended up using PHP to achieve this. still interested in a sql answer










share|improve this question
















I have about 300 records in my database that have invalid data in a column. The columns that are invalid reference a user_id instead of a id code (shown in table).



I'm trying to set the new_foreign_dealer_id = to the user_id inside the old_dealer_id_to_dealer column



So in the table, the user_id with the value 206 would search for 204 and replace 206's row column with 204's new_foreign_dealer_id



Users Table before:



+---------+-----------------------+-------------------------+
| user_id | new_foreign_dealer_id | old_dealer_id_to_delete |
+---------+-----------------------+-------------------------+
| 200 | 5 | 02-000012 |
| 204 | 8 | 02-000097 |
| 206 | 0 (invalid) | 204 (referneces user_id |
+---------+-----------------------+-------------------------+


Users Table after:



+---------+-----------------------+-------------------------+
| user_id | new_foreign_dealer_id | old_dealer_id_to_delete |
+---------+-----------------------+-------------------------+
| 200 | 5 | 02-000012 |
| 204 | 8 | 02-000097 |
| 206 | 8 | 204 (referneces user_id |
+---------+-----------------------+-------------------------+


This is the query I tried



UPDATE users SET dealer_id_foreign = dealer_id_foreign
WHERE dealer_id = user_id



NOTE: The column names in my query are correct. The ASCII columns are for clarity purposes.



edit: ended up using PHP to achieve this. still interested in a sql answer







mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 17:29







millman2394

















asked Nov 14 '18 at 23:07









millman2394millman2394

108112




108112







  • 1





    I think Alex's answer give you a SQL solution. Let know if the query is still not resolved. Otherwise, Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

    – Madhur Bhaiya
    Nov 15 '18 at 20:23











  • what does it take for someone to upvote my question? :P

    – millman2394
    Nov 15 '18 at 21:38











  • Here you go. Upvoted :-)

    – Madhur Bhaiya
    Nov 16 '18 at 4:10











  • thank you!!! :)

    – millman2394
    Nov 16 '18 at 21:05












  • 1





    I think Alex's answer give you a SQL solution. Let know if the query is still not resolved. Otherwise, Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

    – Madhur Bhaiya
    Nov 15 '18 at 20:23











  • what does it take for someone to upvote my question? :P

    – millman2394
    Nov 15 '18 at 21:38











  • Here you go. Upvoted :-)

    – Madhur Bhaiya
    Nov 16 '18 at 4:10











  • thank you!!! :)

    – millman2394
    Nov 16 '18 at 21:05







1




1





I think Alex's answer give you a SQL solution. Let know if the query is still not resolved. Otherwise, Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

– Madhur Bhaiya
Nov 15 '18 at 20:23





I think Alex's answer give you a SQL solution. Let know if the query is still not resolved. Otherwise, Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

– Madhur Bhaiya
Nov 15 '18 at 20:23













what does it take for someone to upvote my question? :P

– millman2394
Nov 15 '18 at 21:38





what does it take for someone to upvote my question? :P

– millman2394
Nov 15 '18 at 21:38













Here you go. Upvoted :-)

– Madhur Bhaiya
Nov 16 '18 at 4:10





Here you go. Upvoted :-)

– Madhur Bhaiya
Nov 16 '18 at 4:10













thank you!!! :)

– millman2394
Nov 16 '18 at 21:05





thank you!!! :)

– millman2394
Nov 16 '18 at 21:05












2 Answers
2






active

oldest

votes


















1














http://sqlfiddle.com/#!9/6e5a88/1



UPDATE my_table t
JOIN my_table new
ON t.old_dealer_id_to_delete = new.user_id
AND t.new_foreign_dealer_id = 0
SET t.new_foreign_dealer_id = new.new_foreign_dealer_id;





share|improve this answer























  • thanks! I can see that works. better than my PHP method

    – millman2394
    Nov 15 '18 at 21:37


















0














Try this I haven't tested it,so please test it first



UPDATE users u
JOIN users ucopy ON u.user_id = ucopy.user_id
SET dealer_id_foreign = dealer_id_foreign
WHERE dealer_id = user_id





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%2f53310094%2fmysql-updating-column-based-on-another-columns-value%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














    http://sqlfiddle.com/#!9/6e5a88/1



    UPDATE my_table t
    JOIN my_table new
    ON t.old_dealer_id_to_delete = new.user_id
    AND t.new_foreign_dealer_id = 0
    SET t.new_foreign_dealer_id = new.new_foreign_dealer_id;





    share|improve this answer























    • thanks! I can see that works. better than my PHP method

      – millman2394
      Nov 15 '18 at 21:37















    1














    http://sqlfiddle.com/#!9/6e5a88/1



    UPDATE my_table t
    JOIN my_table new
    ON t.old_dealer_id_to_delete = new.user_id
    AND t.new_foreign_dealer_id = 0
    SET t.new_foreign_dealer_id = new.new_foreign_dealer_id;





    share|improve this answer























    • thanks! I can see that works. better than my PHP method

      – millman2394
      Nov 15 '18 at 21:37













    1












    1








    1







    http://sqlfiddle.com/#!9/6e5a88/1



    UPDATE my_table t
    JOIN my_table new
    ON t.old_dealer_id_to_delete = new.user_id
    AND t.new_foreign_dealer_id = 0
    SET t.new_foreign_dealer_id = new.new_foreign_dealer_id;





    share|improve this answer













    http://sqlfiddle.com/#!9/6e5a88/1



    UPDATE my_table t
    JOIN my_table new
    ON t.old_dealer_id_to_delete = new.user_id
    AND t.new_foreign_dealer_id = 0
    SET t.new_foreign_dealer_id = new.new_foreign_dealer_id;






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 17:42









    AlexAlex

    15.2k12038




    15.2k12038












    • thanks! I can see that works. better than my PHP method

      – millman2394
      Nov 15 '18 at 21:37

















    • thanks! I can see that works. better than my PHP method

      – millman2394
      Nov 15 '18 at 21:37
















    thanks! I can see that works. better than my PHP method

    – millman2394
    Nov 15 '18 at 21:37





    thanks! I can see that works. better than my PHP method

    – millman2394
    Nov 15 '18 at 21:37













    0














    Try this I haven't tested it,so please test it first



    UPDATE users u
    JOIN users ucopy ON u.user_id = ucopy.user_id
    SET dealer_id_foreign = dealer_id_foreign
    WHERE dealer_id = user_id





    share|improve this answer



























      0














      Try this I haven't tested it,so please test it first



      UPDATE users u
      JOIN users ucopy ON u.user_id = ucopy.user_id
      SET dealer_id_foreign = dealer_id_foreign
      WHERE dealer_id = user_id





      share|improve this answer

























        0












        0








        0







        Try this I haven't tested it,so please test it first



        UPDATE users u
        JOIN users ucopy ON u.user_id = ucopy.user_id
        SET dealer_id_foreign = dealer_id_foreign
        WHERE dealer_id = user_id





        share|improve this answer













        Try this I haven't tested it,so please test it first



        UPDATE users u
        JOIN users ucopy ON u.user_id = ucopy.user_id
        SET dealer_id_foreign = dealer_id_foreign
        WHERE dealer_id = user_id






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 17:36









        Nedko DimitrovNedko Dimitrov

        533716




        533716



























            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%2f53310094%2fmysql-updating-column-based-on-another-columns-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

            Darth Vader #20

            Ondo

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