Update multiple rows with multiple values and multiple conditions mysql

14,828

Solution 1

Try this:

UPDATE your_table SET sales = 
CASE 
    WHEN campid = 259 AND date = 22/6/2011 THEN 200
    WHEN campid = 259 AND date = 21/6/2011 THEN 210
    WHEN campid = 259 AND date = 22/6/2011 THEN 140
    WHEN campid = 259 AND date = 21/6/2011 THEN 150
    ELSE sales
END

Naturally I don't know if date field is really DATE or DATETIME, so I left query showing what you can do, but maybe you have to fix dates comparison according to data type. If date field is DATE (as it should) you can write AND date = '2011-06-22' and so on.
Note ELSE condition: it's necessary to avoid records not falling inside other cases will be set to NULL.

Solution 2

Rather than write a sql query that is far too complicated and time involved, I believe you would be better off spending your time writing a data access object to handle these rather simple manipulations on a per record basis. This makes later maintenance of the code, along with development of new code using your data access objects far easier than a one time use, intricate sql query.

Solution 3

You certainly should not do these in a single query. Instead, if what you aim for is to update them atomically, all at the same time, you should issue several UPDATE statements in a single transaction.

You do not say which MySQL version you use, and not which storage engine. Assuming InnoDB - which is the standard in recent versions of MySQL and should generally be used for transactional systems - and also assuming you are doing this from the command line client, you would

mysql> set autocommit=0;
mysql> UPDATE ....;
mysql> UPDATE ....;
mysql> ...
mysql> commit;

You can then reenable autocommit if you like by repeating the first line, but with a value of 1:

mysql> set autocommit=1;
Share:
14,828

Related videos on Youtube

Nadeem Jamali
Author by

Nadeem Jamali

A professional software developer and architect, algorithm designer and analyst, UI designer, fast learner, self-starter, and self-motivated. I started programming as fun, and then accepted it as my hobby, and ultimately I adopted it as my profession. Through out my career I have been designing and developing software solutions for complex problems, like summarising big data, designing and developing compact and user friendly UI like mobile-first web apps. Being an offshore team member at Cox Automotive, I currently serve as an architect developer to design, develop and maintain summary routines for batch jobs to summarise big data using .Net platform and web applications using asp.net and AngularJS. Prior to dealer.com, I served at KNYSYS on different positions including team lead and senior software developer. I leaded development on different projects up to their successful delivery.

Updated on June 04, 2022

Comments

  • Nadeem Jamali
    Nadeem Jamali almost 2 years

    I am facing a complex situation of SQL queries. The task is to update multiple rows, with multiple values and multiple conditions. Following is the data which I want to update; Field to update: 'sales', condition fields: 'campid' and 'date':

    if campid = 259 and date = 22/6/2011 then set sales = $200
    else if campid = 259 and date = 21/6/2011 then set sales = $210
    else if campid = 260 and date = 22/6/2011 then set sales = $140
    else if campid = 260 and date = 21/6/2011 then set sales = $150
    

    I want to update all these in one query.

  • ajreal
    ajreal over 12 years
    imo, that's not answering what OP asking for
  • Daniel Schneller
    Daniel Schneller over 12 years
    I humbly disagree. The If-elseif-elseif etc. construct presented in the question, coupled with the statement that he wants to update fields all at the same time is exactly what my answer would do. Of course, there are other ways to do this, as well, if one statement is really(!) needed.
  • Nadeem Jamali
    Nadeem Jamali over 12 years
    Daniel is correct. I was asking for multiple conditions for a certain execution.
  • Nadeem Jamali
    Nadeem Jamali about 11 years
    I used this example in my problem, it worked. After a long time, I came on conclusion that this type of query gives best performance on small amount of data. In case if the table has millions of records, then updating single record at a time using primary key in parameter, saves a lot of time, hence produces the better performance.
  • e-info128
    e-info128 about 10 years
    Thanks, I saved the night