Why we mention in MySQL WHERE 1=0?


The condition 1=0 can be used to stop the query from returning any rows. It returns empty set.

The syntax is as follows:

SELECT *FROM yourTableName WHERE 1=0;

To understand the above syntax,let us create a table. The query to create a table is as follows:

mysql> create table ConditionDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(10),
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.59 sec)

Insert some records in the table using insert command. The query is as follows:

mysql> insert into ConditionDemo(Name) values('Larry');
Query OK, 1 row affected (0.10 sec)
mysql> insert into ConditionDemo(Name) values('Sam');
Query OK, 1 row affected (0.14 sec)
mysql> insert into ConditionDemo(Name) values('Mike');
Query OK, 1 row affected (0.13 sec)
mysql> insert into ConditionDemo(Name) values('Carol');
Query OK, 1 row affected (0.12 sec)
mysql> insert into ConditionDemo(Name) values('John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into ConditionDemo(Name) values('Bob');
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement. The query is as follows:

mysql> select *from ConditionDemo;

The following is the output:

+----+-------+
| Id | Name  |
+----+-------+
|  1 | Larry |
|  2 | Sam   |
|  3 | Mike  |
|  4 | Carol |
|  5 | John  |
|  6 | Bob   |
+----+-------+
6 rows in set (0.00 sec)

Here is the query to prevent the query from returning any row

mysql> select *from ConditionDemo where 1=0;
Empty set (0.00 sec)

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements