Between…And
用途:
指定需返回数据的范围
语法:
SELECT column_name FROM table_name WHERE column_name BETWEEN value1 AND 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 |