Why does MySQL evaluate “TRUE or TRUE and FALSE” to true?

MySQLMySQLi Database

MySQL evaluates “TRUE or TRUE and FALSE” to true because AND has the highest priority than OR i.e. AND is evaluated before OR.

The MySQL evaluates the above statement like this. The AND operator gets evaluated first −

(TRUE or (TRUE AND FALSE))

The statement (TRUE AND FALSE) gives the result FALSE. Then the second statement evaluates like this −

(TRUE or FALSE)

The above statement gives the result TRUE.

Let us implement one by one −

mysql> select (TRUE AND FALSE);
+------------------+
| (TRUE AND FALSE) |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)

Now we can put the above result in place of AND condition −

mysql> select (TRUE or FALSE);
+-----------------+
| (TRUE or FALSE) |
+-----------------+
|               1 |
+-----------------+
1 row in set (0.00 sec)

Now check the whole condition once again −

mysql> select (TRUE or TRUE and FALSE);

This will produce the following output −

+--------------------------+
| (TRUE or TRUE and FALSE) |
+--------------------------+
|                        1 |
+--------------------------+
1 row in set (0.00 sec)
raja
Published on 10-Jul-2019 12:09:25
Advertisements