gpt4 book ai didi

mysql - 为什么 (x IS NULL != y IS NULL) 在 MySQL 中需要额外的括号?

转载 作者:行者123 更新时间:2023-11-29 01:52:59 25 4
gpt4 key购买 nike

我有一个 MySQL 表,其字段成组,规则是每行只使用一个组,并且该组的所有(且仅)字段都是非空的。

明确地说,让我们看一下其中的一些字段

x_encrypted
x_hashed
y_encrypted
y_hashed

我有一个触发器,如果​​您插入错误记录,它会发出失败信号。其中一项检查是这个

IF (x_encrypted IS NULL != x_hashed IS NULL) ... error

即x 既被加密又被散列,或者它不存在。

这会在 x_encrypted IS NULL AND x_hashed IS NULL 的情况下触发错误。解决方法是像这样重写它,加上额外的括号:

IF ((x_encrypted IS NULL) != (x_hashed IS NULL)) ... error

为什么需要这些额外的括号?我不明白这在句法上有何歧义。我担心有一个陷阱等着我,我在其他地方有类似的 NULL 相关检查,我不再信任。

最佳答案

根据 documentation , IS!= 具有相同的优先级。还有:

For operators that occur at the same precedence level within an expression, evaluation proceeds left to right, with the exception that assignments evaluate right to left.

这意味着,这个表达式:

x_encrypted IS NULL != x_hashed IS NULL

将被评估为:

((x_encrypted IS NULL) != x_hashed) IS NULL

因此,您必须使用显式括号来覆盖默认的评估顺序。

关于mysql - 为什么 (x IS NULL != y IS NULL) 在 MySQL 中需要额外的括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36392338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com