Vote Up 0 Vote Down

mysql else if update query

I need to update a lot of rows in a mysql. Looping through a php program and updating will bring a performance
overhead. So I decided to use 'if else' statement in mysql query itself instead of php. But I don't know how to do it.
I am expecting the following structure (It is not correct. Just an example).

 update customers_table
set status = if(balance > 1000) { 'active'}
else if(balance == 1000){'waiting'} else {'inactive'}
where country = 'india'


Vote Up 0 Vote Down
Use the following format if you want to try 'else if' condition in mysql.

UPDATE customers_table
WHEN balance > 1000 THEN
WHEN balance = 1000 THEN
WHERE country = 'india';

Here you can give multiple else if conditions. There is no longer to use that check condition in php. It will be
helpful if you are updating thousands of rows with a check condition. Using mysql instead of php here definetly
improve performance.
flag | link |

Your Answer

Login before answering

Login with facebook