当前位置: 首页 > 网络学院 > 服务端脚本教程 > SQL > SQL 与 & OR

SQL
SQL Create View
SQL Server
SQL 速查
SQL 摘要
MYSQL 列类型存储需求

SQL 与 & OR


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 962 ::
收藏到网摘: n/a

AND 与 OR

AND and OR join two or more conditions in a WHERE clause.
AND 和 OR 可以在WHERE子句中将两个过多个条件结合起来

The AND operator displays a row if ALL conditions listed are true. The OR operator displays a row if ANY of the conditions listed are true.
当WHERE字句使用AND操作,那么得让AND所关联的所有条件都为真才会将结果显示出来。 使用OR则只需要其中的一个条件符合就可以将该结果显示出来


Original Table (used in the examples)
初始数据表(在举例中使用)

LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes


举例

Use AND to display each person with the first name equal to "Tove", and the last name equal to "Svendson":
这句使用了AND,数据要显示那么它的first name必须为"Tove"而且last name必须为"Svendson"。

SELECT * FROM Persons
WHERE FirstName='Tove'
AND LastName='Svendson'

结果:

LastName FirstName Address City
Svendson Tove Borgvn 23 Sandnes

举例

Use OR to display each person with the first name equal to "Tove", or the last name equal to "Svendson":
这句使用了OR,数据要显示的话那么它所包含的数据里first name为"Tove"或者last name为"Svendon" :

SELECT * FROM Persons
WHERE firstname='Tove'
OR lastname='Svendson'

结果:

LastName FirstName Address City
Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes

举例

You can also combine AND and OR (use parentheses to form complex expressions):
你还可以将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

评论 (0) All

登陆 | 还没注册?