PostgreSQL多条件求值流程
Evaluation flow of multiple conditions in PostgreSQL
我四处搜索,但还没有找到这个问题的具体答案。
IF x = TRUE AND y = TRUE THEN
-- DO SOMETHING
END IF;
在上面的语句中,plpgsql 是否不考虑 x 的值来评估 y?还是发现x为FALSE后停止?
我知道这是一个简单的问题,所以我可能没有找到简单答案的正确搜索词。如果是这样,请提前致歉。
您不能对求值顺序做出任何假设:
The order of evaluation of subexpressions is not defined. In particular, the inputs of an operator or function are not necessarily evaluated left-to-right or in any other fixed order.
Postgres 足够聪明,一旦知道答案就停止评估。
if the result of an expression can be determined by evaluating only some parts of it, then other subexpressions might not be evaluated at all.
必要时可以控制执行顺序。根据上下文,有几种方法可以解决这个问题,每种方法都有自己的局限性。例如,可以在查询中使用 CASE 语句。
请阅读:https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-EXPRESS-EVAL
我四处搜索,但还没有找到这个问题的具体答案。
IF x = TRUE AND y = TRUE THEN
-- DO SOMETHING
END IF;
在上面的语句中,plpgsql 是否不考虑 x 的值来评估 y?还是发现x为FALSE后停止?
我知道这是一个简单的问题,所以我可能没有找到简单答案的正确搜索词。如果是这样,请提前致歉。
您不能对求值顺序做出任何假设:
The order of evaluation of subexpressions is not defined. In particular, the inputs of an operator or function are not necessarily evaluated left-to-right or in any other fixed order.
Postgres 足够聪明,一旦知道答案就停止评估。
if the result of an expression can be determined by evaluating only some parts of it, then other subexpressions might not be evaluated at all.
必要时可以控制执行顺序。根据上下文,有几种方法可以解决这个问题,每种方法都有自己的局限性。例如,可以在查询中使用 CASE 语句。
请阅读:https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-EXPRESS-EVAL