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

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

SQL Between


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

BETWEEN ... AND

The BETWEEN ... AND operator selects a range of data between two values. These values can be numbers, text, or dates.
使用BETWEEN...AND 操作符可以选中排列于两值之间的数据。这些数据可以是数字,文字或是日期。

SELECT column_name FROM table_name WHERE column_name BETWEEN value1 AND value2


Original Table (used in the examples)
初始表

LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Nordmann Anna Neset 18 Sandnes
Pettersen Kari Storgt 20 Stavanger
Svendson Tove Borgvn 23 Sandnes


举例 1

To display the persons alphabetically between (and including) "Hansen" and exclusive "Pettersen", use the following SQL:
下面这条SQL语句会列举出LastName经过字母排序后包含在"Hansen"(包括在内)到"Petteren"(不包括)之间的所有Persons信息:

SELECT * FROM Persons WHERE LastName 
BETWEEN 'Hansen' AND 'Pettersen'

结果:

LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Nordmann Anna Neset 18 Sandnes

IMPORTANT! The BETWEEN...AND operator is treated differently in different databases. With some databases a person with the LastName of "Hansen" or "Pettersen" will not be listed (BETWEEN..AND only selects fields that are between and excluding the test values). With some databases a person with the last name of "Hansen" or "Pettersen" will be listed (BETWEEN..AND selects fields that are between and including the test values). With other databases a person with the last name of "Hansen" will be listed, but "Pettersen" will not be listed (BETWEEN..AND selects fields between the test values, including the first test value and excluding the last test value). Therefore: Check how your database treats the BETWEEN....AND operator!
重要信息!不同的数据库系统它们的BETWEEN...AND操作符的处理不一样。一些数据库即使该数据有"Hansen"或是有"Pettersen"也不会显示出一条来(它只会显示出两值之内的数据)。而有些数据库会把两个都显示出来,还有些是后者会显示,而前者不被包含在内。


举例 2

To display the persons outside the range used in the previous example, use the NOT operator:
使用了NOT操作符的语句会将包括在两者范围内的数据排除在外

SELECT * FROM Persons WHERE LastName 
NOT BETWEEN 'Hansen' AND 'Pettersen'

结果:

LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger
Svendson Tove Borgvn 23 Sandnes

评论 (0) All

登陆 | 还没注册?