这个问题已经在这里有了答案 :
SQL逻辑运算符优先级:和和或 (4个答案)
7年前关闭。
我有以下查询:
SELECT * FROM MUFFIN_TYPES MT WHERE MT.flavor IS NULL OR MT.flavor IS NOT NULL AND MT.additionDate IS NOT NULL
如果不弄清楚括号,这是令人困惑的。会MT.flavor IS NULL OR MT.flavor IS NOT NULL永远评估为真吗?还是将其评估为MT.flavor IS NULL (OR MT.flavor IS NOT NULL AND MT.additionDate IS NOT NULL)?
MT.flavor IS NULL OR MT.flavor IS NOT NULL
MT.flavor IS NULL (OR MT.flavor IS NOT NULL AND MT.additionDate IS NOT NULL)
我找不到有关此事的任何文档。我是两个选项中的后者,但我不确定。
注意:我正在使用Oracle Pl / SQL。
AND的优先级高于OR:
来自Oracle 12.1文档 (AND和OR的优先级至少与版本7一致- 可能更进一步,但我没有文档链接)
先前版本:
运算符的优先级显示在下面的列表中,从最高优先级到最低优先级。一起显示在一行上的运算符具有相同的优先级。 INTERVAL BINARY, COLLATE ! - (unary minus), ~ (unary bit inversion) ^ *, /, DIV, %, MOD -, + <<, >> & | = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN BETWEEN, CASE, WHEN, THEN, ELSE NOT &&, AND XOR ||, OR = (assignment), :=
运算符的优先级显示在下面的列表中,从最高优先级到最低优先级。一起显示在一行上的运算符具有相同的优先级。
INTERVAL BINARY, COLLATE ! - (unary minus), ~ (unary bit inversion) ^ *, /, DIV, %, MOD -, + <<, >> & | = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN BETWEEN, CASE, WHEN, THEN, ELSE NOT &&, AND XOR ||, OR = (assignment), :=
但是,我强烈建议使用括号,以确保清楚并确保按您期望的顺序对运算符进行评估。