SQL statement: modify data in condition

case expression:

Case  When  < Analyzing Expression >  the then  < expression > 
     When  < Analyzing Expression >  the then  < expression > 
     When  < Analyzing Expression >  the then  < expression > 
     ... 
     the else  < Expression > 
End

Update statements need to use update statement:

update table set column name = value (modified WHERE  < Analyzing expression > )

example:

Given a salary table, as shown below, there are m = f = male and female values. F and m are all exchange value (e.g., change the value of m f all, and vice versa). It requires only one update (the Update) statement, and no intermediate temporary table. (You can only write a update statement can not write the select statement)

id name sex salary
1 A m 2500 
2 B f 1500
3 C m 5500
4 D f 500

answer:

update salary
set sex = (case sex
                when 'm' then 'f'
                else 'm'
           end)

Guess you like

Origin www.cnblogs.com/wulizhazha/p/12090835.html