I have a query like this:
IF ( FALSE AND (SELECT 1 FROM mytable WHERE id = :id) ) THEN
/* INSERT something here */
ENDIF;
All I'm trying to know is whether that SELECT
statement which is into that condition will be executed or not?
As you know, that INSERT
statement never run, because there is FALSE
and AND
in the condition, so the condition is always FALSE
.
Now I just want to know that SELECT
executes before that IF
-statement? I'm saying that because I heard everything which is into parenthesis will be executed first in MySQL.
The documentation (MySQL :: 13.3.1 Operator Precedence) doesn't say it but it seems a short-circuit is followed. It would be necessary to confirm the information.
MariaDB gives more details: MariaDB :: Operator Precedence :: Short-circuit evaluation.
Example:
mysql> SET @`out` := 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT (FALSE AND (SELECT @`out` := 0));
+----------------------------------+
| (FALSE AND (SELECT @`out` := 0)) |
+----------------------------------+
| 0 |
+----------------------------------+
1 row in set (0.00 sec)
mysql> SELECT @`out`;
+--------+
| @`out` |
+--------+
| 1 | -- <-- Not change
+--------+
1 row in set (0.00 sec)
mysql> SELECT (TRUE AND (SELECT @`out` := 0));
+---------------------------------+
| (TRUE AND (SELECT @`out` := 0)) |
+---------------------------------+
| 0 |
+---------------------------------+
1 row in set (0.00 sec)
mysql> SELECT @`out`;
+--------+
| @`out` |
+--------+
| 0 | -- <-- Change
+--------+
1 row in set (0.00 sec)
UPDATE
mysql> SET @`out` := 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT (TRUE AND (SELECT @`out` := 1) AND FALSE);
+-------------------------------------------+
| (TRUE AND (SELECT @`out` := 1) AND FALSE) |
+-------------------------------------------+
| 0 |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT @`out`;
+--------+
| @`out` |
+--------+
| 1 | -- <-- Change
+--------+
1 row in set (0.00 sec)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments