括号里的东西会先执行吗?

Will be executed everything which is into parenthesis first?

我有这样的查询:

IF ( FALSE AND (SELECT 1 FROM mytable WHERE id = :id) ) THEN

    /* INSERT something here */

ENDIF;

我想知道的是进入那个条件的 SELECT 语句是否会被执行?

如你所知,那个INSERT语句永远不会运行,因为条件中有FALSEAND,所以条件总是FALSE.

现在我只想知道 SELECTIF 语句之前执行?我这么说是因为我听说括号中的所有内容都将首先在 MySQL.

中执行

关于您的代码:它会按照您的预期执行。它将执行 SELECT 然后评估链接到您的 IF

的条件

许多编程语言都有这种行为。如有疑问,请使用括号。

文档 (MySQL :: 13.3.1 Operator Precedence) 没有说明,但似乎发生了短路。有必要确认信息。

MariaDB 提供了更多细节:MariaDB :: Operator Precedence :: Short-circuit evaluation.

示例:

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)

更新

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)