tags:

views:

87

answers:

2

I know I can easily do this with a few lines of PHP but I was hoping there was a method in MYSQL to do this; I would think there is but I am not having any luck finding it.

Basically I want to do this:

UPDATE fieldName SET status='1'
if status='1', SET status='2'

So that second line is obviously not real code, but that is what I want to do. UPDATE a field to a value, and if that field already equals that value update it to a different value.

+2  A: 

Would this work?

UPDATE table SET status = IF(status = 1, 2, 1) WHERE ...;

Basically, that's if status is equal to 1, then set it to 2 otherwise, leave it was 1. But then you might as well do, if that's all you want to do:

UPDATE table SET status = 2 WHERE status = 1;
Darryl Hein
Cheers. Knew it was something obvious!
Chris Cooper
+1  A: 

If you want to have a field that acts like a toggle button try this:

UPDATE table SET status = ABS(status - 1) WHERE...

This way every time you update the status toggles between 0 and 1

Keeper
This would work too! I'll remember it in the future.
Chris Cooper