3

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?

John Woo
  • 258,903
  • 69
  • 498
  • 492
madhur
  • 357
  • 2
  • 6
  • 17

3 Answers3

7

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')
John Woo
  • 258,903
  • 69
  • 498
  • 492
5

The shortest solution I see:

UPDATE Trade 
SET call = CONCAT(call,'E')
WHERE call IN ('C', 'P');
sashkello
  • 17,306
  • 24
  • 81
  • 109
4

Yes, you can do this in one query, using the CASE expression. Something like this:

update Trade 
set call = CASE 
             WHEN callputtype='C' THEN 'CE' 
             WHEN callputtype='P' THEN 'PE' 
           END
where callputtype IN ('c', 'P');
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164