MySQL Forums
Forum List  »  Newbie

Re: WHERE TRUE/FALSE - what's the sense of this statement?
Posted by: Rick James
Date: May 12, 2014 09:11AM

The WHERE clause is evaluated for each row. If it evaluates to TRUE, then the row is delivered by the SELECT.

Therefore, the first statement will deliver all the rows; the second will deliver none, thereby resulting in an empty resultset.

(Actually the optimizer will throw away WHERE TRUE and simply do "SELECT * FROM test;" for the first one. For the second, it will optimize away the entire statement.)

Using my 2-row table `dbl`, I see that EXPLAIN says:
mysql> EXPLAIN select * from dbl where true;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | dbl   | ALL  | NULL          | NULL | NULL    | NULL |    2 | NULL  |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

mysql> EXPLAIN select * from dbl where false;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra            |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
1 row in set (0.00 sec)
The first says it will look at All rows.
The second says "Impossible WHERE", implying that it is always FALSE, so no rows will be fetched; furthermore it even does not bother touching the table (= NULL).

Have I answered your question? Or was there something deeper in it?

Options: ReplyQuote


Subject
Written By
Posted
Re: WHERE TRUE/FALSE - what's the sense of this statement?
May 12, 2014 09:11AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.