And & Or
用途:
在WHERE子句中AND和OR被用来连接两个或者更多的条件
解释:
AND在结合两个布尔表达式时,只有在两个表达式都为 TRUE 时才传回 TRUE
OR在结合两个布尔表达式时,只要其中一个条件为 TRUE 时,OR便传回 TRUE
例:
"Persons" 表中的原始数据:
LastName |
FirstName |
Address |
City |
Hansen |
Ola |
Timoteivn 10 |
Sandnes |
Svendson |
Tove |
Borgvn 23 |
Sandnes |
Svendson |
Stephen |
Kaivn 18 |
Sandnes |
用AND运算子来查找"Persons" 表中FirstName为”Tove”而且LastName为” Svendson”的数据
SELECT * FROM Persons WHERE FirstName='Tove' AND LastName='Svendson' |
返回结果:
LastName |
FirstName |
Address |
City |
Svendson |
Tove |
Borgvn 23 |
Sandnes |
用OR运算子来查找"Persons" 表中FirstName为”Tove”或者LastName为” Svendson”的数据
SELECT * FROM Persons WHERE firstname='Tove' OR lastname='Svendson' |
返回结果:
LastName |
FirstName |
Address |
City |
Svendson |
Tove |
Borgvn 23 |
Sandnes |
Svendson |
Stephen |
Kaivn 18 |
Sandnes |
你也能结合AND和OR (使用括号形成复杂的表达式),如:
SELECT * FROM Persons WHERE (FirstName='Tove' OR FirstName='Stephen') AND LastName='Svendson' |
返回结果:
LastName |
FirstName |
Address |
City |
Svendson |
Tove |
Borgvn 23 |
Sandnes |
Svendson |
Stephen |
Kaivn 18 |
Sandnes |