Mysql: Updating column based on another columns value
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
add a comment |
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
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
add a comment |
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
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
mysql
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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;
thanks! I can see that works. better than my PHP method
– millman2394
Nov 15 '18 at 21:37
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
thanks! I can see that works. better than my PHP method
– millman2394
Nov 15 '18 at 21:37
add a comment |
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;
thanks! I can see that works. better than my PHP method
– millman2394
Nov 15 '18 at 21:37
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 15 '18 at 17:36
Nedko DimitrovNedko Dimitrov
533716
533716
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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