简体   繁体   中英

UPDATE query for multiple rows of same column

I have a database table and What I required to do is that,

I need to update the column with the column name 'Co15' of every rows according to the following conditions

Co15 = SAMPLE if Co13 = 'c1' AND Col2 = 'b4'
Co15 = LIST if Co13 = 'c6'

Currently I am running each update query separately as follows

UPDATE tblname SET Co15 = 'SAMPLE' WHERE Co13 = 'c1' AND Col2 = 'b4';                                   

UPDATE tblname SET Co15 = 'LIST' WHERE Co13 = 'c6';

But wanted to know if there is any way where I could run only one update query all at once.

Thanks

Try this

UPDATE tblname SET Co15=  
CASE
  WHEN Co13 = 'c1' AND Col2='b4' THEN 'SAMPLE'
  WHEN Co13 = 'c6' THEN 'LIST'
END

exactly getting the output to as following as:

UPDATE tblname
    SET col5= CASE
        WHEN col3 = 'c1' AND col2 = 'b4' THEN 'SAMPL'
        WHEN col3 = 'c6' THEN 'LIST'
    END

example: sqlfiddle to click here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM