Thursday, 15 May 2014

Perform if-then-else-if action on column in MySQL -



Perform if-then-else-if action on column in MySQL -

this follow-up question compare 2 columns, perform action on column in mysql

i have table named tablex in mysql. there 4 columns in tablex. columns columncompare_now, columncompare_past, columnnumber_now, columnnumber_past.

i want write mysql update statement has next logic;

if columncompare_now == 'actionneeded' , columncompare_past == 'actionneeded', columnnumber_now = `columnnumber_now` + `columnnumber_past` else if columncompare_now == 'actionneeded' , columncompare_past != 'actionneeded' columnnumber_now = `columnnumber_now` - `columnnumber_past`

how can else if statement handled in mysql?

for conditional update can utilize case

update tablex set columnnumber_now = case when columncompare_now = 'actionneeded' , columncompare_past = 'actionneeded' `columnnumber_now` + `columnnumber_past` when columncompare_now = 'actionneeded' , columncompare_past != 'actionneeded' `columnnumber_now` - `columnnumber_past` end

mysql

No comments:

Post a Comment