布尔运算符的意外行为
Unexpected behavior of boolean operators
为什么许多语言(据我所知)按顺序处理条件和按位布尔运算符 and
然后 or
(像往常一样),但是 MS SQL Server 和 Postgre SQL 没有?
Java脚本
a = true || true && true && false; // true
a = true | true & true & false; // 1
a = 1 | 1 & 1 & 0; // 1
Java
a = true || true && true && false; // true
a = true | true & true & false; // true
a = 1 | 1 & 1 & 0; // 1
Python
a = True or True and True and False // true
a = 1 | 1 & 1 & 0 // 1
Delphi
a := true or true and true and false; // true
a := 1 or 1 and 1 and 0; // 1
PHP
$a = true | true & true & false; // 1
$a = true || true && true && false; // 1
c(CentOS下的gcc)
a = true || true && true && false; // true
a = true | true & true & false; // true
a = 1 || 1 && 1 && 0; // 1
a = 1 | 1 & 1 & 0; // 1
MySQL
select 1 | 1 & 1 & 0 a -- 1
select true | true & true & false -- 1
select true || true && true && false -- 1
SQL 服务器
select 1 | 1 & 1 & 0 a -- 0 !
select cast(1 as bit) | cast(1 as bit) &
cast(1 as bit) & cast(0 as bit) -- 0 !
PostgreSQL
select true or true and true and false a -- true
select 1 | 1 & 1 & 0 a -- 0 !
select 1::bit | 1::bit & 1::bit & 0::bit a -- 0 !
的| SQL 中的 & 运算符是 bitwise operators, not logical operators. Bitwise | and & have equal precedence in SQL Server, in MySQL & is higher than |.
SQL中的逻辑运算符有and
、or
等
为什么许多语言(据我所知)按顺序处理条件和按位布尔运算符 and
然后 or
(像往常一样),但是 MS SQL Server 和 Postgre SQL 没有?
Java脚本
a = true || true && true && false; // true
a = true | true & true & false; // 1
a = 1 | 1 & 1 & 0; // 1
Java
a = true || true && true && false; // true
a = true | true & true & false; // true
a = 1 | 1 & 1 & 0; // 1
Python
a = True or True and True and False // true
a = 1 | 1 & 1 & 0 // 1
Delphi
a := true or true and true and false; // true
a := 1 or 1 and 1 and 0; // 1
PHP
$a = true | true & true & false; // 1
$a = true || true && true && false; // 1
c(CentOS下的gcc)
a = true || true && true && false; // true
a = true | true & true & false; // true
a = 1 || 1 && 1 && 0; // 1
a = 1 | 1 & 1 & 0; // 1
MySQL
select 1 | 1 & 1 & 0 a -- 1
select true | true & true & false -- 1
select true || true && true && false -- 1
SQL 服务器
select 1 | 1 & 1 & 0 a -- 0 !
select cast(1 as bit) | cast(1 as bit) &
cast(1 as bit) & cast(0 as bit) -- 0 !
PostgreSQL
select true or true and true and false a -- true
select 1 | 1 & 1 & 0 a -- 0 !
select 1::bit | 1::bit & 1::bit & 0::bit a -- 0 !
的| SQL 中的 & 运算符是 bitwise operators, not logical operators. Bitwise | and & have equal precedence in SQL Server, in MySQL & is higher than |.
SQL中的逻辑运算符有and
、or
等