Solved

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

Posted on 2009-05-05
3
253 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:jatkin
[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 500 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:jatkin
ID: 31578080
Excellent, thanks!
That saves a heck of a lot of unnecessary calls! :-)
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.

752 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