Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to toggle boolean value in MySQL [duplicate]

Tags:

mysql

Possible Duplicate:
Is there a way in MySQL to reverse a boolean field with one query?

To update (boolean) value normally we would check if it's set to false or true, and update it, I was wondering if there's query that would toggle boolean value.

like image 300
Santosh Linkha Avatar asked Feb 06 '11 11:02

Santosh Linkha


People also ask

How do you toggle boolean value in SQL?

You can use Boolean Operator for this Here delete is your boolean field. This solution also works for none boolean fields such as int and tinyint where values are set to 0 or 1.

How do you toggle a boolean?

To toggle a boolean, use the strict inequality (! ==) operator to compare the boolean to true , e.g. bool !== true . The comparison will return false if the boolean value is equal to true and vice versa, effectively toggling the boolean.

How do you set a boolean to true in MySQL?

You can update boolean value using UPDATE command. If you use the BOOLEAN data type, MySQL internally convert it into tinyint(1). It can takes true or false literal in which true indicates 1 to tinyint(1) and false indicates 0 to tinyint(1).


2 Answers

UPDATE mytbl    SET field = !field  WHERE id = 42 

Where 42 is the id of the record, field is the name of the boolean field and mytbl is the table name.

like image 66
zerkms Avatar answered Sep 27 '22 15:09

zerkms


You can use Boolean Operator for this Here delete is your boolean field.

update tab set `delete`=NOT `delete` 
like image 32
XMen Avatar answered Sep 27 '22 16:09

XMen