
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Is there a way in MySQL to reverse a boolean field with a single query?
Yes, you can use if() function from MySQL to reverse a boolean field. The syntax is as follows −
UPDATE yourTableName SET yourBooleanColumnName = IF(yourBooleanColumnName,0,1);
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table ReverseBooleanField -> ( -> Id int NOT NULL AUTO_INCREMENT, -> isValid BOOLEAN -> , -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.56 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into ReverseBooleanField(isValid) values(true); Query OK, 1 row affected (0.18 sec) mysql> insert into ReverseBooleanField(isValid) values(false); Query OK, 1 row affected (0.14 sec) mysql> insert into ReverseBooleanField(isValid) values(true); Query OK, 1 row affected (0.29 sec) mysql> insert into ReverseBooleanField(isValid) values(true); Query OK, 1 row affected (0.09 sec) mysql> insert into ReverseBooleanField(isValid) values(false); Query OK, 1 row affected (0.14 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from ReverseBooleanField;
The following is the output −
+----+---------+ | Id | isValid | +----+---------+ | 1 | 1 | | 2 | 0 | | 3 | 1 | | 4 | 1 | | 5 | 0 | +----+---------+ 5 rows in set (0.00 sec)
MySQL internally converts BOOLEAN to TINYINT(1) which gives true as 1 and false as 0.
Now, let us reverse boolean field in a single query −
mysql> update ReverseBooleanField set isValid = if(isValid,0,1); Query OK, 5 rows affected (0.21 sec) Rows matched − 5 Changed − 5 Warnings − 0
Check whether the boolean field is reversed or not using select statement. The query is as follows −
mysql> select *from ReverseBooleanField;
The following is the output −
+----+---------+ | Id | isValid | +----+---------+ | 1 | 0 | | 2 | 1 | | 3 | 0 | | 4 | 0 | | 5 | 1 | +----+---------+ 5 rows in set (0.00 sec)
- Related Articles
- Count boolean field values within a single MySQL query?
- Is there any easy way to add multiple records in a single MySQL query?
- MySQL query to fetch only a single field on the basis of boolean value in another field
- Fastest way to insert with multiple values in a single MySQL query?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Is there a way to convert Integer field into Varchar without losing data in MySQL?
- MySQL query to update only a single field in place of NULL
- MongoDB query for a single field
- Add a single day to datetime field with MySQL INTERVAL
- Update two columns with a single MySQL query
- Is there a way to know your current username in MySQL?
- Is there a way to make a list from a MySQL table in Java?
- How to bulk update MySQL data with a single query?
- Is there a way to select a value which matches partially in MySQL?
- Is there an easy way to rename a table in a MySQL procedure?

Advertisements