SQL update multiple values in a newly added column?
I have this database in MySQl named friends, and let's say I have three rows in the Friends table.
ID Name Age Email(newly added column)
1 Jane 22
2 Melissa 23
3 Andrew 23
Now I want to add emails to each of the person in the database using this SQL syntax below, but it doesn't work. Where did I go wrong?
Update friends
set email= 'jane@abc.com' where id = 1,
set email= 'Melissa@abc.com' where id = 2,
set email= 'Andrew@abc.com' where id = 3;
mysql sql
add a comment |
I have this database in MySQl named friends, and let's say I have three rows in the Friends table.
ID Name Age Email(newly added column)
1 Jane 22
2 Melissa 23
3 Andrew 23
Now I want to add emails to each of the person in the database using this SQL syntax below, but it doesn't work. Where did I go wrong?
Update friends
set email= 'jane@abc.com' where id = 1,
set email= 'Melissa@abc.com' where id = 2,
set email= 'Andrew@abc.com' where id = 3;
mysql sql
Since you are using MySQL, why did you add Oracle and Microsoft SQL Server tags to your question? Answers may depend on the DBMS you are using.
– Dan Guzman
Nov 16 '18 at 3:37
add a comment |
I have this database in MySQl named friends, and let's say I have three rows in the Friends table.
ID Name Age Email(newly added column)
1 Jane 22
2 Melissa 23
3 Andrew 23
Now I want to add emails to each of the person in the database using this SQL syntax below, but it doesn't work. Where did I go wrong?
Update friends
set email= 'jane@abc.com' where id = 1,
set email= 'Melissa@abc.com' where id = 2,
set email= 'Andrew@abc.com' where id = 3;
mysql sql
I have this database in MySQl named friends, and let's say I have three rows in the Friends table.
ID Name Age Email(newly added column)
1 Jane 22
2 Melissa 23
3 Andrew 23
Now I want to add emails to each of the person in the database using this SQL syntax below, but it doesn't work. Where did I go wrong?
Update friends
set email= 'jane@abc.com' where id = 1,
set email= 'Melissa@abc.com' where id = 2,
set email= 'Andrew@abc.com' where id = 3;
mysql sql
mysql sql
edited Nov 16 '18 at 3:57
Gordon Linoff
789k35314418
789k35314418
asked Nov 16 '18 at 3:35
user71812user71812
1097
1097
Since you are using MySQL, why did you add Oracle and Microsoft SQL Server tags to your question? Answers may depend on the DBMS you are using.
– Dan Guzman
Nov 16 '18 at 3:37
add a comment |
Since you are using MySQL, why did you add Oracle and Microsoft SQL Server tags to your question? Answers may depend on the DBMS you are using.
– Dan Guzman
Nov 16 '18 at 3:37
Since you are using MySQL, why did you add Oracle and Microsoft SQL Server tags to your question? Answers may depend on the DBMS you are using.
– Dan Guzman
Nov 16 '18 at 3:37
Since you are using MySQL, why did you add Oracle and Microsoft SQL Server tags to your question? Answers may depend on the DBMS you are using.
– Dan Guzman
Nov 16 '18 at 3:37
add a comment |
2 Answers
2
active
oldest
votes
You need to do it 3 different queries like:
Update friends set email= 'jane@abc.com' where id = 1;
Update friends set email= 'Melissa@abc.com' where id = 2;
Update friends set email= 'Andrew@abc.com' where id = 3;
OR use case when expression:
Update friends set email= (case when id=1 then 'jane@abc.com'
when id=2 then 'melissa@abc.com'
when id=3 then 'andrew@abc.com' end)
Ah, I see. Thanks a lot Eray for clarifying, your solution works perfectly well!
– user71812
Nov 16 '18 at 3:41
Seemscase when
missingend
keyword
– lucumt
Nov 16 '18 at 3:41
1
Glad to help user 71812. Will appreciate if you select the answer as the correct answer by clicking the tick on the left @user71812
– Eray Balkanli
Nov 16 '18 at 3:42
add a comment |
Your statements are correct. Just remove the commas and run these statements separately.
Update friends set email= 'jane@abc.com' where id = 1;
Update friends set email= 'Melissa@abc.com' where id = 2;
Update friends set email= 'Andrew@abc.com' where id = 3;
Since my answer got down-voted, could someone please suggest as to how could have I written a better answer?
– Namandeep_Kaur
Nov 17 '18 at 0:03
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%2f53331055%2fsql-update-multiple-values-in-a-newly-added-column%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
You need to do it 3 different queries like:
Update friends set email= 'jane@abc.com' where id = 1;
Update friends set email= 'Melissa@abc.com' where id = 2;
Update friends set email= 'Andrew@abc.com' where id = 3;
OR use case when expression:
Update friends set email= (case when id=1 then 'jane@abc.com'
when id=2 then 'melissa@abc.com'
when id=3 then 'andrew@abc.com' end)
Ah, I see. Thanks a lot Eray for clarifying, your solution works perfectly well!
– user71812
Nov 16 '18 at 3:41
Seemscase when
missingend
keyword
– lucumt
Nov 16 '18 at 3:41
1
Glad to help user 71812. Will appreciate if you select the answer as the correct answer by clicking the tick on the left @user71812
– Eray Balkanli
Nov 16 '18 at 3:42
add a comment |
You need to do it 3 different queries like:
Update friends set email= 'jane@abc.com' where id = 1;
Update friends set email= 'Melissa@abc.com' where id = 2;
Update friends set email= 'Andrew@abc.com' where id = 3;
OR use case when expression:
Update friends set email= (case when id=1 then 'jane@abc.com'
when id=2 then 'melissa@abc.com'
when id=3 then 'andrew@abc.com' end)
Ah, I see. Thanks a lot Eray for clarifying, your solution works perfectly well!
– user71812
Nov 16 '18 at 3:41
Seemscase when
missingend
keyword
– lucumt
Nov 16 '18 at 3:41
1
Glad to help user 71812. Will appreciate if you select the answer as the correct answer by clicking the tick on the left @user71812
– Eray Balkanli
Nov 16 '18 at 3:42
add a comment |
You need to do it 3 different queries like:
Update friends set email= 'jane@abc.com' where id = 1;
Update friends set email= 'Melissa@abc.com' where id = 2;
Update friends set email= 'Andrew@abc.com' where id = 3;
OR use case when expression:
Update friends set email= (case when id=1 then 'jane@abc.com'
when id=2 then 'melissa@abc.com'
when id=3 then 'andrew@abc.com' end)
You need to do it 3 different queries like:
Update friends set email= 'jane@abc.com' where id = 1;
Update friends set email= 'Melissa@abc.com' where id = 2;
Update friends set email= 'Andrew@abc.com' where id = 3;
OR use case when expression:
Update friends set email= (case when id=1 then 'jane@abc.com'
when id=2 then 'melissa@abc.com'
when id=3 then 'andrew@abc.com' end)
edited Nov 16 '18 at 3:41
answered Nov 16 '18 at 3:37
Eray BalkanliEray Balkanli
4,47452346
4,47452346
Ah, I see. Thanks a lot Eray for clarifying, your solution works perfectly well!
– user71812
Nov 16 '18 at 3:41
Seemscase when
missingend
keyword
– lucumt
Nov 16 '18 at 3:41
1
Glad to help user 71812. Will appreciate if you select the answer as the correct answer by clicking the tick on the left @user71812
– Eray Balkanli
Nov 16 '18 at 3:42
add a comment |
Ah, I see. Thanks a lot Eray for clarifying, your solution works perfectly well!
– user71812
Nov 16 '18 at 3:41
Seemscase when
missingend
keyword
– lucumt
Nov 16 '18 at 3:41
1
Glad to help user 71812. Will appreciate if you select the answer as the correct answer by clicking the tick on the left @user71812
– Eray Balkanli
Nov 16 '18 at 3:42
Ah, I see. Thanks a lot Eray for clarifying, your solution works perfectly well!
– user71812
Nov 16 '18 at 3:41
Ah, I see. Thanks a lot Eray for clarifying, your solution works perfectly well!
– user71812
Nov 16 '18 at 3:41
Seems
case when
missing end
keyword– lucumt
Nov 16 '18 at 3:41
Seems
case when
missing end
keyword– lucumt
Nov 16 '18 at 3:41
1
1
Glad to help user 71812. Will appreciate if you select the answer as the correct answer by clicking the tick on the left @user71812
– Eray Balkanli
Nov 16 '18 at 3:42
Glad to help user 71812. Will appreciate if you select the answer as the correct answer by clicking the tick on the left @user71812
– Eray Balkanli
Nov 16 '18 at 3:42
add a comment |
Your statements are correct. Just remove the commas and run these statements separately.
Update friends set email= 'jane@abc.com' where id = 1;
Update friends set email= 'Melissa@abc.com' where id = 2;
Update friends set email= 'Andrew@abc.com' where id = 3;
Since my answer got down-voted, could someone please suggest as to how could have I written a better answer?
– Namandeep_Kaur
Nov 17 '18 at 0:03
add a comment |
Your statements are correct. Just remove the commas and run these statements separately.
Update friends set email= 'jane@abc.com' where id = 1;
Update friends set email= 'Melissa@abc.com' where id = 2;
Update friends set email= 'Andrew@abc.com' where id = 3;
Since my answer got down-voted, could someone please suggest as to how could have I written a better answer?
– Namandeep_Kaur
Nov 17 '18 at 0:03
add a comment |
Your statements are correct. Just remove the commas and run these statements separately.
Update friends set email= 'jane@abc.com' where id = 1;
Update friends set email= 'Melissa@abc.com' where id = 2;
Update friends set email= 'Andrew@abc.com' where id = 3;
Your statements are correct. Just remove the commas and run these statements separately.
Update friends set email= 'jane@abc.com' where id = 1;
Update friends set email= 'Melissa@abc.com' where id = 2;
Update friends set email= 'Andrew@abc.com' where id = 3;
answered Nov 16 '18 at 3:43
Namandeep_KaurNamandeep_Kaur
12517
12517
Since my answer got down-voted, could someone please suggest as to how could have I written a better answer?
– Namandeep_Kaur
Nov 17 '18 at 0:03
add a comment |
Since my answer got down-voted, could someone please suggest as to how could have I written a better answer?
– Namandeep_Kaur
Nov 17 '18 at 0:03
Since my answer got down-voted, could someone please suggest as to how could have I written a better answer?
– Namandeep_Kaur
Nov 17 '18 at 0:03
Since my answer got down-voted, could someone please suggest as to how could have I written a better answer?
– Namandeep_Kaur
Nov 17 '18 at 0:03
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%2f53331055%2fsql-update-multiple-values-in-a-newly-added-column%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
Since you are using MySQL, why did you add Oracle and Microsoft SQL Server tags to your question? Answers may depend on the DBMS you are using.
– Dan Guzman
Nov 16 '18 at 3:37