Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Simple way to toggle a value of an int field

Tags:

mysql

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?

like image 496
fmsf Avatar asked Mar 02 '09 20:03

fmsf


People also ask

How do I change a field value in MySQL?

To replace, use the REPLACE() MySQL function. Since you need to update the table for this, use the UPDATE() function with the SET clause.

How do I select a specific field in MySQL?

If you want to select only specific columns, replace the * with the names of the columns, separated by commas. The following statement selects just the name_id, firstname and lastname fields from the master_name table.

How do I select between values in MySQL?

The MySQL BETWEEN OperatorThe BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included.

Which command is used for changing values in MySQL table?

UPDATE `table_name` is the command that tells MySQL to update the data in a table . SET `column_name` = `new_value' are the names and values of the fields to be affected by the update query.


2 Answers

UPDATE table SET field = 1 - field 
like image 76
FDisk Avatar answered Sep 20 '22 13:09

FDisk


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

like image 37
vartec Avatar answered Sep 21 '22 13:09

vartec