Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to only show database items not chosen from initial captured multi select 11 51
Parts and Products table schema in mysql 6 53
update joined tables 2 49
category table 2 25
Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 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