Tuesday, May 29, 2018

sql - MYSQL Update Single column with different values





There is this table "Trade" with column "call",Call contains values(C,P) when loaded from CSV
I want to update Trade from java program such that if call='C' then call='CE' and



if call='P' then call='PE'



I figured out this can be done using 2 queries.like this



update Trade set call='CE' where call='C';

update Trade set call='PE' where call='P';



is there anyway this can be done in single query?


Answer



CALL is a reserved keyword and needs to be escaped.



UPDATE  Trade
SET `CALL` = CASE WHEN `Call` = 'C' THEN 'CE'
WHEN `Call` = 'P' THEN 'PE'
ELSE `CALL` END

WHERE `CALL` IN ('C','P')

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...