How to update multiple values in mysql based on another table using join The 2019 Stack Overflow Developer Survey Results Are InAdd a column with a default value to an existing table in SQL ServerSQL update from one Table to another based on a ID matchCan I concatenate multiple MySQL rows into one field?How to get a list of MySQL user accountsHow can I do an UPDATE statement with JOIN in SQL?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableInsert into a MySQL table or update if existsHow to reset AUTO_INCREMENT in MySQL?How to import an SQL file using the command line in MySQL?
What do I do when my TA workload is more than expected?
Why not take a picture of a closer black hole?
Getting crown tickets for Statue of Liberty
Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past
How come people say “Would of”?
If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?
Time travel alters history but people keep saying nothing's changed
A word that means fill it to the required quantity
Can an undergraduate be advised by a professor who is very far away?
How to type this arrow in math mode?
What is the motivation for a law requiring 2 parties to consent for recording a conversation
Match Roman Numerals
Accepted by European university, rejected by all American ones I applied to? Possible reasons?
How to notate time signature switching consistently every measure
How can I have a shield and a way of attacking with a ranged weapon at the same time?
Why was M87 targeted for the Event Horizon Telescope instead of Sagittarius A*?
What information about me do stores get via my credit card?
What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?
Is bread bad for ducks?
What is this sharp, curved notch on my knife for?
Why couldn't they take pictures of a closer black hole?
How to translate "being like"?
Is it okay to consider publishing in my first year of PhD?
Does HR tell a hiring manager about salary negotiations?
How to update multiple values in mysql based on another table using join
The 2019 Stack Overflow Developer Survey Results Are InAdd a column with a default value to an existing table in SQL ServerSQL update from one Table to another based on a ID matchCan I concatenate multiple MySQL rows into one field?How to get a list of MySQL user accountsHow can I do an UPDATE statement with JOIN in SQL?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableInsert into a MySQL table or update if existsHow to reset AUTO_INCREMENT in MySQL?How to import an SQL file using the command line in MySQL?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have two tables products and filters. My products table is like below:-
id name color_id size_id composition_id
1 Test Black 60x60 CM Cotton
2 Test2 Red 60X90 CM Acryllic
My filters table as below:-
id filter_name
1 Black
2 Red
22 60x60 CM
23 60X90 CM
61 Cotton
62 Acryllic
My Expected output like below:-
id name color_id size_id composition_id
1 Test 1 22 61
2 Test2 2 23 62
I have tried below query that is working for only color_id. Query is below:-
UPDATE products
INNER JOIN filters ON products.color_id = filters.filter_name
SET products.color_id = filters.id
I want to update all ' color_id,size_id,composition_id' in one update statement. May be we need to use Case Statements. Can anyone help?
mysql sql
add a comment |
I have two tables products and filters. My products table is like below:-
id name color_id size_id composition_id
1 Test Black 60x60 CM Cotton
2 Test2 Red 60X90 CM Acryllic
My filters table as below:-
id filter_name
1 Black
2 Red
22 60x60 CM
23 60X90 CM
61 Cotton
62 Acryllic
My Expected output like below:-
id name color_id size_id composition_id
1 Test 1 22 61
2 Test2 2 23 62
I have tried below query that is working for only color_id. Query is below:-
UPDATE products
INNER JOIN filters ON products.color_id = filters.filter_name
SET products.color_id = filters.id
I want to update all ' color_id,size_id,composition_id' in one update statement. May be we need to use Case Statements. Can anyone help?
mysql sql
add a comment |
I have two tables products and filters. My products table is like below:-
id name color_id size_id composition_id
1 Test Black 60x60 CM Cotton
2 Test2 Red 60X90 CM Acryllic
My filters table as below:-
id filter_name
1 Black
2 Red
22 60x60 CM
23 60X90 CM
61 Cotton
62 Acryllic
My Expected output like below:-
id name color_id size_id composition_id
1 Test 1 22 61
2 Test2 2 23 62
I have tried below query that is working for only color_id. Query is below:-
UPDATE products
INNER JOIN filters ON products.color_id = filters.filter_name
SET products.color_id = filters.id
I want to update all ' color_id,size_id,composition_id' in one update statement. May be we need to use Case Statements. Can anyone help?
mysql sql
I have two tables products and filters. My products table is like below:-
id name color_id size_id composition_id
1 Test Black 60x60 CM Cotton
2 Test2 Red 60X90 CM Acryllic
My filters table as below:-
id filter_name
1 Black
2 Red
22 60x60 CM
23 60X90 CM
61 Cotton
62 Acryllic
My Expected output like below:-
id name color_id size_id composition_id
1 Test 1 22 61
2 Test2 2 23 62
I have tried below query that is working for only color_id. Query is below:-
UPDATE products
INNER JOIN filters ON products.color_id = filters.filter_name
SET products.color_id = filters.id
I want to update all ' color_id,size_id,composition_id' in one update statement. May be we need to use Case Statements. Can anyone help?
mysql sql
mysql sql
asked Mar 22 at 4:53
kunalkunal
12210
12210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
One way you can do this is with a set of correlated subqueries, one for each column that you need to look up. We use a COALESCE
so that if the value is not found, we retain the previous value:
UPDATE products p
SET color_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.color_id), color_id),
size_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.size_id), size_id),
composition_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.composition_id), composition_id)
Then you can SELECT * FROM products
:
id name color_id size_id composition_id
1 Test 1 22 61
2 Test2 2 23 62
Demo on dbfiddle
You can also achieve the same result with an UPDATE ... JOIN
query, which might be more efficient:
UPDATE products p
LEFT JOIN filters f1 ON f1.filter_name = p.color_id
LEFT JOIN filters f2 ON f2.filter_name = p.size_id
LEFT JOIN filters f3 ON f3.filter_name = p.composition_id
SET color_id = COALESCE(f1.id, color_id),
size_id = COALESCE(f2.id, size_id),
composition_id = COALESCE(f3.id, composition_id)
Demo on dbfiddle
Your way is right but what if my 100 records are already updated and i again run this query... above 100 records will be empty .. Hope you understand
– kunal
Mar 22 at 5:07
@kunal I didn't realise you might run the query multiple times. I've updated it (and the demo) for that situation
– Nick
Mar 22 at 5:12
actually my client don't want to fill ids in column.. So my client gives csv sheet with names every week so i will update the sql query manually . Hope you understand
– kunal
Mar 22 at 5:15
1
@kunal no worries. Glad I could help.
– Nick
Mar 22 at 5:21
1
COALESCE
looks at each of its arguments, starting from the left, and returns the first one that is notNULL
. So for example, in the second query, if there is no match forcolor_id
infilters
,f1.id
will beNULL
and theCOALESCE(f1.id, color_id)
will return the original value ofcolor_id
– Nick
Mar 22 at 5:25
|
show 4 more comments
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%2f55293120%2fhow-to-update-multiple-values-in-mysql-based-on-another-table-using-join%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
One way you can do this is with a set of correlated subqueries, one for each column that you need to look up. We use a COALESCE
so that if the value is not found, we retain the previous value:
UPDATE products p
SET color_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.color_id), color_id),
size_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.size_id), size_id),
composition_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.composition_id), composition_id)
Then you can SELECT * FROM products
:
id name color_id size_id composition_id
1 Test 1 22 61
2 Test2 2 23 62
Demo on dbfiddle
You can also achieve the same result with an UPDATE ... JOIN
query, which might be more efficient:
UPDATE products p
LEFT JOIN filters f1 ON f1.filter_name = p.color_id
LEFT JOIN filters f2 ON f2.filter_name = p.size_id
LEFT JOIN filters f3 ON f3.filter_name = p.composition_id
SET color_id = COALESCE(f1.id, color_id),
size_id = COALESCE(f2.id, size_id),
composition_id = COALESCE(f3.id, composition_id)
Demo on dbfiddle
Your way is right but what if my 100 records are already updated and i again run this query... above 100 records will be empty .. Hope you understand
– kunal
Mar 22 at 5:07
@kunal I didn't realise you might run the query multiple times. I've updated it (and the demo) for that situation
– Nick
Mar 22 at 5:12
actually my client don't want to fill ids in column.. So my client gives csv sheet with names every week so i will update the sql query manually . Hope you understand
– kunal
Mar 22 at 5:15
1
@kunal no worries. Glad I could help.
– Nick
Mar 22 at 5:21
1
COALESCE
looks at each of its arguments, starting from the left, and returns the first one that is notNULL
. So for example, in the second query, if there is no match forcolor_id
infilters
,f1.id
will beNULL
and theCOALESCE(f1.id, color_id)
will return the original value ofcolor_id
– Nick
Mar 22 at 5:25
|
show 4 more comments
One way you can do this is with a set of correlated subqueries, one for each column that you need to look up. We use a COALESCE
so that if the value is not found, we retain the previous value:
UPDATE products p
SET color_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.color_id), color_id),
size_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.size_id), size_id),
composition_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.composition_id), composition_id)
Then you can SELECT * FROM products
:
id name color_id size_id composition_id
1 Test 1 22 61
2 Test2 2 23 62
Demo on dbfiddle
You can also achieve the same result with an UPDATE ... JOIN
query, which might be more efficient:
UPDATE products p
LEFT JOIN filters f1 ON f1.filter_name = p.color_id
LEFT JOIN filters f2 ON f2.filter_name = p.size_id
LEFT JOIN filters f3 ON f3.filter_name = p.composition_id
SET color_id = COALESCE(f1.id, color_id),
size_id = COALESCE(f2.id, size_id),
composition_id = COALESCE(f3.id, composition_id)
Demo on dbfiddle
Your way is right but what if my 100 records are already updated and i again run this query... above 100 records will be empty .. Hope you understand
– kunal
Mar 22 at 5:07
@kunal I didn't realise you might run the query multiple times. I've updated it (and the demo) for that situation
– Nick
Mar 22 at 5:12
actually my client don't want to fill ids in column.. So my client gives csv sheet with names every week so i will update the sql query manually . Hope you understand
– kunal
Mar 22 at 5:15
1
@kunal no worries. Glad I could help.
– Nick
Mar 22 at 5:21
1
COALESCE
looks at each of its arguments, starting from the left, and returns the first one that is notNULL
. So for example, in the second query, if there is no match forcolor_id
infilters
,f1.id
will beNULL
and theCOALESCE(f1.id, color_id)
will return the original value ofcolor_id
– Nick
Mar 22 at 5:25
|
show 4 more comments
One way you can do this is with a set of correlated subqueries, one for each column that you need to look up. We use a COALESCE
so that if the value is not found, we retain the previous value:
UPDATE products p
SET color_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.color_id), color_id),
size_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.size_id), size_id),
composition_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.composition_id), composition_id)
Then you can SELECT * FROM products
:
id name color_id size_id composition_id
1 Test 1 22 61
2 Test2 2 23 62
Demo on dbfiddle
You can also achieve the same result with an UPDATE ... JOIN
query, which might be more efficient:
UPDATE products p
LEFT JOIN filters f1 ON f1.filter_name = p.color_id
LEFT JOIN filters f2 ON f2.filter_name = p.size_id
LEFT JOIN filters f3 ON f3.filter_name = p.composition_id
SET color_id = COALESCE(f1.id, color_id),
size_id = COALESCE(f2.id, size_id),
composition_id = COALESCE(f3.id, composition_id)
Demo on dbfiddle
One way you can do this is with a set of correlated subqueries, one for each column that you need to look up. We use a COALESCE
so that if the value is not found, we retain the previous value:
UPDATE products p
SET color_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.color_id), color_id),
size_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.size_id), size_id),
composition_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.composition_id), composition_id)
Then you can SELECT * FROM products
:
id name color_id size_id composition_id
1 Test 1 22 61
2 Test2 2 23 62
Demo on dbfiddle
You can also achieve the same result with an UPDATE ... JOIN
query, which might be more efficient:
UPDATE products p
LEFT JOIN filters f1 ON f1.filter_name = p.color_id
LEFT JOIN filters f2 ON f2.filter_name = p.size_id
LEFT JOIN filters f3 ON f3.filter_name = p.composition_id
SET color_id = COALESCE(f1.id, color_id),
size_id = COALESCE(f2.id, size_id),
composition_id = COALESCE(f3.id, composition_id)
Demo on dbfiddle
edited Mar 22 at 5:11
answered Mar 22 at 5:05
NickNick
39.4k132443
39.4k132443
Your way is right but what if my 100 records are already updated and i again run this query... above 100 records will be empty .. Hope you understand
– kunal
Mar 22 at 5:07
@kunal I didn't realise you might run the query multiple times. I've updated it (and the demo) for that situation
– Nick
Mar 22 at 5:12
actually my client don't want to fill ids in column.. So my client gives csv sheet with names every week so i will update the sql query manually . Hope you understand
– kunal
Mar 22 at 5:15
1
@kunal no worries. Glad I could help.
– Nick
Mar 22 at 5:21
1
COALESCE
looks at each of its arguments, starting from the left, and returns the first one that is notNULL
. So for example, in the second query, if there is no match forcolor_id
infilters
,f1.id
will beNULL
and theCOALESCE(f1.id, color_id)
will return the original value ofcolor_id
– Nick
Mar 22 at 5:25
|
show 4 more comments
Your way is right but what if my 100 records are already updated and i again run this query... above 100 records will be empty .. Hope you understand
– kunal
Mar 22 at 5:07
@kunal I didn't realise you might run the query multiple times. I've updated it (and the demo) for that situation
– Nick
Mar 22 at 5:12
actually my client don't want to fill ids in column.. So my client gives csv sheet with names every week so i will update the sql query manually . Hope you understand
– kunal
Mar 22 at 5:15
1
@kunal no worries. Glad I could help.
– Nick
Mar 22 at 5:21
1
COALESCE
looks at each of its arguments, starting from the left, and returns the first one that is notNULL
. So for example, in the second query, if there is no match forcolor_id
infilters
,f1.id
will beNULL
and theCOALESCE(f1.id, color_id)
will return the original value ofcolor_id
– Nick
Mar 22 at 5:25
Your way is right but what if my 100 records are already updated and i again run this query... above 100 records will be empty .. Hope you understand
– kunal
Mar 22 at 5:07
Your way is right but what if my 100 records are already updated and i again run this query... above 100 records will be empty .. Hope you understand
– kunal
Mar 22 at 5:07
@kunal I didn't realise you might run the query multiple times. I've updated it (and the demo) for that situation
– Nick
Mar 22 at 5:12
@kunal I didn't realise you might run the query multiple times. I've updated it (and the demo) for that situation
– Nick
Mar 22 at 5:12
actually my client don't want to fill ids in column.. So my client gives csv sheet with names every week so i will update the sql query manually . Hope you understand
– kunal
Mar 22 at 5:15
actually my client don't want to fill ids in column.. So my client gives csv sheet with names every week so i will update the sql query manually . Hope you understand
– kunal
Mar 22 at 5:15
1
1
@kunal no worries. Glad I could help.
– Nick
Mar 22 at 5:21
@kunal no worries. Glad I could help.
– Nick
Mar 22 at 5:21
1
1
COALESCE
looks at each of its arguments, starting from the left, and returns the first one that is not NULL
. So for example, in the second query, if there is no match for color_id
in filters
, f1.id
will be NULL
and the COALESCE(f1.id, color_id)
will return the original value of color_id
– Nick
Mar 22 at 5:25
COALESCE
looks at each of its arguments, starting from the left, and returns the first one that is not NULL
. So for example, in the second query, if there is no match for color_id
in filters
, f1.id
will be NULL
and the COALESCE(f1.id, color_id)
will return the original value of color_id
– Nick
Mar 22 at 5:25
|
show 4 more comments
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%2f55293120%2fhow-to-update-multiple-values-in-mysql-based-on-another-table-using-join%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