tags:

views:

457

answers:

6

I know how to do this, but i think I'll overcomplicate it with double selects and so on.

How can you do this (example in pseudo-sql)

UPDATE some_table SET an_int_value = (an_int_value==1 ? 0 : 1);

It must be an int value due to some other functionality, but how do you do it in a simple way?

+5  A: 
UPDATE some_table SET an_int_value = IF(an_int_value=1, 0, 1);

http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#function_if

vartec
hehe, you were faster by 23 sec ;)
glavić
yeah, with those obvious question there are always few people responding at the same time ;-)
vartec
The one who pays more attention to SO (or writes faster) gets rewarded :P
fmsf
@fmsf: You are correct, so vote up for @vartec ;)
glavić
+6  A: 
UPDATE some_table SET an_int_value = IF(an_int_value=1, 0, 1)
glavić
+2  A: 

In this case, you could use an XOR type operation:

UPDATE some_table SET an_int_value = an_int_value XOR 1

This is assuming that an_int_value will always be 1 or 0 though.

Eric Petroelje
+1  A: 

Another option:

UPDATE some_table SET an_int_value = ABS(an_int_value - 1);
Chad Birch
+2  A: 
UPDATE table SET field = 1 - field
FDisk
A: 

I can see the answers of all experienced people and i too got updated with their answers.

what about this... i do this way...

UPDATE tablename SET fieldname = not fieldname

can any body give suggestions please if this will not be a feasible solution. with respect to execution speed or any other... what to say... fact... concept... .

Jayapal Chandran