Can you check for both a blank string and 0 in one condition with a single MySQL query?


Yes, we can check for a blank string and 0 in a single condition. Let us first create a table −

mysql> create table DemoTable
(
   ClientId varchar(40)
);
Query OK, 0 rows affected (1.01 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('CLI-01');
Query OK, 1 row affected (0.36 sec)
mysql> insert into DemoTable values('0');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('CLI-02');
Query OK, 1 row affected (0.31 sec)
mysql> insert into DemoTable values('CLI-00');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('CLI-98');
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+
| ClientId |
+----------+
| CLI-01   |
| 0        |
| CLI-02   |
| CLI-00   |
|          |
| CLI-98   |
+----------+
6 rows in set (0.00 sec)

Following is the query to check for both a blank string and 0 in a single condition −

mysql> select *from DemoTable where ClientId IN('0','');

This will produce the following output −

+----------+
| ClientId |
+----------+
| 0        |
|          |
+----------+
2 rows in set (0.00 sec)

Updated on: 10-Oct-2019

41 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements