功能:BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围。这些值可以是数值、文本或者日期。
SQL BETWEEN语法:
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2
使用实例:
如需以字母顺序显示介于 "Adams"(包括)和 "Carter"(不包括)之间的人,请使用下面的 SQL:
SELECT * FROM Persons WHERE LastName BETWEEN 'Adams' AND 'Carter'
选择ASP数据库内的user列里所有含值在字母ab与de之间的数据。
SELECT * FROM asp WHERE user BETWEEN 'ab' AND 'de'
如需使用上面的例子显示范围之外的人,请使用 NOT 操作符:
SELECT * FROM Persons WHERE LastName NOT BETWEEN 'Adams' AND 'Carter'
再来些例子:
日期段的查询
SELECT ID, Name, RegisterDate FROM Customer WHERE (RegisterDate BETWEEN '2007-01-06' AND '2007-01-06')
查询一定范围的价格
USE Library SELECT * FROM BookInfo WHERE price BETWEEN 35 AND 60
查询ID在一定范围的数据
SELECT * FROM suppliers WHERE id between 5000 AND 5010