【SQL语法】系列05:AND与OR

AND 和 OR 可在 WHERE 子句中把两个或多个条件结合起来。

表:

LastName

FirstName

Address

City

Adams

John

Oxford Street

London

Bush

George

Fifth Avenue

New York

Carter

Thomas

Changan Street

Beijing

Carter

William

Xuanwumen 10

Beijing

AND实例

使用 AND 来显示所有 姓为 "Carter" 并且名为 "Thomas" 的人:

SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter'

结果:

LastName

FirstName

Address

City

Carter

Thomas

Changan Street

Beijing

OR实例

使用 OR 来显示所有 姓为 "Carter" 或者名为 "Thomas" 的人:

SELECT * FROM Persons WHERE firstname='Thomas' OR lastname='Carter'

结果:

LastName

FirstName

Address

City

Carter

Thomas

Changan Street

Beijing

Carter

William

Xuanwumen 10

Beijing

 

也可以把 AND 和 OR 结合起来(使用圆括号来组成复杂的表达式):

SELECT * FROM Persons WHERE (FirstName='Thomas' OR FirstName='William')

AND LastName='Carter'

结果:

LastName

FirstName

Address

City

Carter

Thomas

Changan Street

Beijing

Carter

William

Xuanwumen 10

Beijing

 

你可能感兴趣的:(sql)