What is “where 1=1” statement in MySQL?

MySQLMySQLi Database

In MySQL “Where 1=1” results in all the rows of a table as this statement is always true. An example to better unerstand this statement is given as follows −

First, a table is created with the help of the create command. This is given as follows −

mysql> CREATE table WhereConditon
-> (
-> id int,
-> name varchar(100)
-> );
Query OK, 0 rows affected (0.43 sec)

After successfully creating a table, some records are inserted with the help of insert command The query for this is given as follows −

mysql> INSERT into WhereConditon values(1,'John');
Query OK, 1 row affected (0.16 sec)

mysql> INSERT into WhereConditon values(2,'Smith');
Query OK, 1 row affected (0.15 sec)

mysql> INSERT into WhereConditon values(3,'Bob');
Query OK, 1 row affected (0.16 sec)

mysql> INSERT into WhereConditon values(4,'David');
Query OK, 1 row affected (0.13 sec)

Now that the records are inserted successfully, the number of records in the table can be checked with the help of the select statement. This is given as follows −

mysql> SELECT * from WhereConditon;

After executing the above query, all the records from the table can be seen as follows −

+------+-------+
| id   | name  |
+------+-------+
| 1    | John  |
| 2    | Smith |
| 3    | Bob   |
| 4    | David |
+------+-------+
4 rows in set (0.00 sec)

Now, the statement 1=1 is used along with the select statement to display the names in the table. All the names will be displayed as 1=1 is always true.

The query for this is given as follows −

mysql> select name from WhereConditon where 1=1;

The following is the output of the above query

+-------+
| name  |
+-------+
| John  |
| Smith |
| Bob   |
| David |
+-------+
4 rows in set (0.00 sec)
raja
Published on 18-Oct-2018 13:03:10
Advertisements