and
都为真时才为真
- /*选择customers的所有列
- 对每位顾客都会检查where子句
- 满足where条件的顾客会在结果表中返回
- (其中birth_date大于1990-01-01,并且points大于1000)*/
- SELECT *
- FROM Customers
- WHERE birth_date > '1990-01-01' AND points > 1000

or: 只要有一个结果正确就在结果表中返回
- /*选择customers的所有列
- 对每位顾客都会检查where子句
- 满足where条件的顾客会在结果表中返回
- or:只要有一个结果正确就在结果表中返回
- (其中birth_date大于1990-01-01,并且points大于1000)*/
- SELECT *
- FROM Customers
- WHERE birth_date > '1990-01-01' OR points > 1000

and的优先级大于or
- /*我们想要获得
- 1990年后出生或者points大于1000并且位于va的顾客*/
- SELECT *
- FROM Customers
- WHERE birth_date > '1990-01-01' OR points > 1000 AND state='va'
- /*and优先评估
- 执行查询的时候
- 首先评估points > 1000 and state='va'这个条件
- */
- /*可以使用括号便于理解*/
- SELECT *
- FROM Customers
- WHERE birth_date > '1990-01-01' OR
- (points > 1000 AND state='va')

not逻辑运算符,用于否定一个条件
- /*我们想要获得
- 1990年前出生且points小于1000并且位于va的顾客*/
- SELECT *
- FROM Customers
- WHERE NOT (birth_date > '1990-01-01' OR points > 1000)
- /*数学小技巧
- 非(P或Q)=非P且非Q
- 每当你用not运算符
- 可以简化表达式
- 否定birth_date > '1990-01-01'条件即birth_date <= '1990-01-01'
- 类似的,我们可以得到下面等价的代码
- */
- SELECT *
- FROM Customers
- WHERE birth_date <= '1990-01-01' and points <= 1000
-
