Will be executed everything which is into parenthesis first?

Martin AJ

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.

wchiquito

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.

edited at
0

Comments

0 comments
Login to comment

Related

@After seems to be executed first before everything else

In Java, which gets executed first, "+" or "++"?

which file is executed first in pytest?

Capture everything enclosed by parenthesis

Not getting the first character of the string and get the data which is in a parenthesis using regex

If there are 2 always blocks which block will be executed first

Regex - everything but anything within parenthesis

Will an instruction/statement which comes before other/s guaranteed to be executed first?

in Backbone model, which method gets executed first, success or parse?

Which will be executed first, added column to entity or flyway migration?

Match first parenthesis with Python

How to delete everything inside a parenthesis without deleting the parenthesis in Vim?

Recursive regex for matching everything in parenthesis (PCRE)

Regular expression in oracle to match everything between parenthesis

Regex to get everything except strings between parenthesis

Match everything until parenthesis with digits inside

Match words which are not inside parenthesis

Regex get first occurence with parenthesis

Excel 2016 copy everything after the first character which repeats itself in excel VBA

If I have two substring_index, which one gets executed first?

Which constructor will be executed?

Regex to match everything from newline upto parenthesis along with a search term

How do you remove everything within parenthesis in BQSQL?

Callback function executed first

what is executed first in c?

Change the values of string in parenthesis which starts with Varchar

Batch - Quotes and parenthesis in variables which are used in IF statements

How to remove parenthesis and everything in between in a string in R where the parenthesis could be anywhere in the string?

Remove everything contained by a pair of parenthesis including the parenthesis itself in case they enclose a specific substring