返回结果:
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
Between…And
用途:
指定需返回数据的范围
语法:
SELECT column_name FROM table_name WHERE column_name BETWEEN value1AND value2
例:“Persons”表中的原始数据
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Nordmann
Anna
Neset 18
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Svendson
Tove
Borgvn 23
Sandnes
用BETWEEN…AND返回LastName为从”Hansen”到”Pettersen”的数据:
SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'
返回结果
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Nordmann
Anna
Neset 18
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
为了显示指定范围之外的数据,也可以用NOT操作符:
SELECT * FROM Persons WHERE LastName NOT BETWEEN 'Hansen' AND 'Pettersen'
返回结果
LastName
FirstName
Address
City
Svendson
Tove
Borgvn 23
Sandnes
Distinct
用途:
DISTINCT关键字被用作返回唯一的值
语法:
SELECT DISTINCT column-name(s) FROM table-name
解释:当column-name(s)中存在重复的值时,返回结果仅留下一个
例:
“Orders”表中的原始数据
Company
OrderNumber
Sega
3412
W3Schools
2312
Trio
4678
W3Schools
6798
用DISTINCT关键字返回Company字段中唯一的值:
SELECT DISTINCT Company FROM Orders
返回结果
Company
Sega
W3Schools
Trio
Order by
用途:
指定结果集的排序
语法:
SELECT column-name(s) FROM table-name ORDER BY { order_by_expression [ ASC | DESC ] }
解释:指定结果集的排序,可以按照ASC(递增方式排序,从最低值到最高值)或者DESC(递减方式排序,从最高值到最低值)的方式进行排序,默认的方式是ASC
例:
“Orders”表中的原始数据:
Company
OrderNumber
Sega
3412
ABC Shop
5678
W3Schools
2312
W3Schools
6798
按照Company字段的升序方式返回结果集:
SELECT Company, OrderNumber