当前位置: 首页 > 网络学院 > 服务端脚本教程 > SQL > SQL Between
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 |
LastName | FirstName | Address | City |
---|---|---|---|
Hansen | Ola | Timoteivn 10 | Sandnes |
Nordmann | Anna | Neset 18 | Sandnes |
Pettersen | Kari | Storgt 20 | Stavanger |
Svendson | Tove | Borgvn 23 | Sandnes |
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 |
结果:
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"也不会显示出一条来(它只会显示出两值之内的数据)。而有些数据库会把两个都显示出来,还有些是后者会显示,而前者不被包含在内。
To display the persons outside the range used in the previous example, use the NOT operator:
使用了NOT操作符的语句会将包括在两者范围内的数据排除在外
SELECT * FROM Persons WHERE LastName |
结果:
LastName | FirstName | Address | City |
---|---|---|---|
Pettersen | Kari | Storgt 20 | Stavanger |
Svendson | Tove | Borgvn 23 | Sandnes |