Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can I execute multiple UPDATE commands in a single SQL Query?

Posted on 2009-05-05
3
Medium Priority
?
255 Views
Last Modified: 2012-05-06
Hi,

I have a MySQL table for which I would like to update several lines on a single update. This would be:

UPDATE myTable SET Field1='A' WHERE Field2 like '123' and MyID = 0;
UPDATE myTable SET Field1='B' WHERE Field2 like '123' and MyID = 1;

Can this be done in a single SQL query in MySQL 5.0? (I actually need to execute 20 each time!)

Any help would be appreciated.

Thanks,

James
0
Comment
Question by:James Atkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 25

Expert Comment

by:reb73
ID: 24306194
Separate each updatable field with a comma, like this -

UPDATE myTable SET
Field1='A',  
Field1='B'
WHERE Field2 like '123' and MyID = 0;
0
 
LVL 25

Accepted Solution

by:
reb73 earned 2000 total points
ID: 24306240
Actually, ignore my previous post  (24306194), i misinterpreted your question..

It would be more like this -

UPDATE myTable SET
      Field1= case when myid = 1 then 'A'
                   when myid = 2 then 'B'
                   when myid = 3 then 'C'
                   -- and so on
                   else Field1
              end
WHERE Field2 like '123';
0
 
LVL 4

Author Closing Comment

by:James Atkin
ID: 31578080
Excellent, thanks!
That saves a heck of a lot of unnecessary calls! :-)
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question