Solved

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help with a query 8 51
VARCHAR vs. CHAR 6 59
Should I use a relational design for this? 3 53
mysql Encryption with PHP 8 49
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Creating and Managing Databases with phpMyAdmin in cPanel.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now