Tuesday 29 May 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...