Solved

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

Posted on 2009-05-05
3
252 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

740 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