您要打印的文件是:SQL的详细语法介绍——对于学习数据库最基础知识二

SQL的详细语法介绍——对于学习数据库最基础知识二

作者:CLNARA    转贴自:CSDN    点击数:4360


Where

用途:

被用来规定一种选择查询的标准

语法:

SELECT column FROM table WHERE column condition value

下面的操作符能被使用在WHERE中:

=,<>,>,<,>=,<=,BETWEEN,LIKE

注意: 在某些SQL的版本中不等号< >能被写作为!=

解释:

  SELECT语句返回WHERE子句中条件为true的数据

例:

Persons”表中选出生活在” Sandnes” 的人

SELECT * FROM Persons WHERE City='Sandnes'

 Persons 表中的数据有:

LastName

FirstName

Address

City

Year

Hansen

Ola

Timoteivn 10

Sandnes

1951

Svendson

Tove

Borgvn 23

Sandnes

1978

Svendson

Stale

Kaivn 18

Sandnes

1980

Pettersen

Kari

Storgt 20

Stavanger

1960

返回结果:

LastName

FirstName

Address

City

Year

Hansen

Ola

Timoteivn 10

Sandnes

1951

Svendson

Tove

Borgvn 23

Sandnes

1978

Svendson

Stale

Kaivn 18

Sandnes

1980

 

 

 

And & Or

用途:

WHERE子句中ANDOR被用来连接两个或者更多的条件

解释:

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

  你也能结合ANDOR (使用括号形成复杂的表达式),:

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