SQL BETWEEN

SQL BETWEEN 操作符

BETWEEN 操作符选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期。

SQL BETWEEN 语法

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

演示数据库

在本教程中,我们将使用 Web3 样本数据库。

下面是选自 "Websites" 表的数据:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 芝麻教程      | http://www.web3.xin/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
| 7  | stackoverflow | http://stackoverflow.com/ |   0 | IND     |
+----+---------------+---------------------------+-------+---------+

BETWEEN 操作符实例

下面的 SQL 语句选取 alexa 介于 1 和 20 之间的所有网站:

实例

SELECT * FROM Websites
WHERE alexa BETWEEN 1 AND 20; 
执行输出结果:
mysql> select * from websites where alexa between 1 and 20;
+----+----------+---------------------------+-------+---------+
| id | name     | url                       | alexa | country |
+----+----------+---------------------------+-------+---------+
|  1 | Google   | https://www.google.cm/    | 1     | USA     |
|  2 | 淘宝     | https://www.taobao.com/   | 13    | CN      |
|  4 | 微博     | http://weibo.com/         | 20    | CN      |
|  5 | Facebook | https://www.facebook.com/ | 3     | USA     |
+----+----------+---------------------------+-------+---------+
4 rows in set (0.00 sec)

NOT BETWEEN 操作符实例

如需显示不在上面实例范围内的网站,请使用 NOT BETWEEN:

实例

SELECT * FROM Websites
WHERE alexa NOT BETWEEN 1 AND 20; 
执行输出结果:
mysql> select * from websites where alexa not between 1 and 20;
+----+----------+----------------------+-------+---------+
| id | name     | url                  | alexa | country |
+----+----------+----------------------+-------+---------+
|  3 | 芝麻教程 | http://www.web3.xin/ | 5000  | USA     |
+----+----------+----------------------+-------+---------+
1 row in set (0.00 sec)
带有 IN 的 BETWEEN 操作符实例

下面的 SQL 语句选取alexa介于 1 和 20 之间但 country 不为 USA 和 IND 的所有网站:

实例

SELECT * FROM Websites
WHERE (alexa BETWEEN 1 AND 20)
AND NOT country IN ('USA', 'IND'); 
执行输出结果:
mysql> select * from websites where (alexa between 1 and 20) And not country in('USA','IND');
+----+------+-------------------------+-------+---------+
| id | name | url                     | alexa | country |
+----+------+-------------------------+-------+---------+
|  2 | 淘宝 | https://www.taobao.com/ | 13    | CN      |
|  4 | 微博 | http://weibo.com/       | 20    | CN      |
+----+------+-------------------------+-------+---------+
2 rows in set (0.00 sec)

带有文本值的 BETWEEN 操作符实例

下面的 SQL 语句选取 name 以介于 'A' 和 'H' 之间字母开始的所有网站:

实例

SELECT * FROM Websites
WHERE name BETWEEN 'A' AND 'H'; 
执行输出结果:
mysql> SELECT * FROM Websites  WHERE name BETWEEN 'A' AND 'H';
+----+----------+---------------------------+-------+---------+
| id | name     | url                       | alexa | country |
+----+----------+---------------------------+-------+---------+
|  1 | Google   | https://www.google.cm/    | 1     | USA     |
|  5 | Facebook | https://www.facebook.com/ | 3     | USA     |
+----+----------+---------------------------+-------+---------+
2 rows in set (0.00 sec)

带有文本值的 NOT BETWEEN 操作符实例

下面的 SQL 语句选取 name 不介于 'A' 和 'H' 之间字母开始的所有网站:

实例

SELECT * FROM Websites
WHERE name NOT BETWEEN 'A' AND 'H'; 
执行输出结果:
mysql> SELECT * FROM Websites  WHERE name NOT BETWEEN 'A' AND 'H';
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  2 | 淘宝          | https://www.taobao.com/   | 13    | CN      |
|  3 | 芝麻教程      | http://www.web3.xin/      | 5000  | USA     |
|  4 | 微博          | http://weibo.com/         | 20    | CN      |
|  9 | stackoverflow | http://stackoverflow.com/ | NULL  | IND     |
+----+---------------+---------------------------+-------+---------+
4 rows in set (0.00 sec)

示例表

下面是 "access_log" 网站访问记录表的数据,其中:

  • aid:为自增 id。
  • site_id:为对应 websites表的网站 id。
  • count:访问次数。
  • date:为访问日期。
mysql> SELECT * FROM access_log;
+-----+---------+-------+------------+
| aid | site_id | count | date       |
+-----+---------+-------+------------+
|   1 |       1 |    45 | 2016-05-10 |
|   2 |       3 |   100 | 2016-05-13 |
|   3 |       1 |   230 | 2016-05-14 |
|   4 |       2 |    10 | 2016-05-14 |
|   5 |       5 |   205 | 2016-05-14 |
|   6 |       4 |    13 | 2016-05-15 |
|   7 |       3 |   220 | 2016-05-15 |
|   8 |       5 |   545 | 2016-05-16 |
|   9 |       3 |   201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)
本教程使用到的 access_log 表 SQL 文件:access_log.sql

带有日期值的 BETWEEN 操作符实例

下面的 SQL 语句选取 date 介于 '2016-05-10' 和 '2016-05-14' 之间的所有访问记录:

实例

SELECT * FROM access_log
WHERE date BETWEEN '2016-05-10' AND '2016-05-14'; 
执行输出结果:
mysql> SELECT * FROM access_log   WHERE date BETWEEN '2016-05-10' AND '2016-05-14';
+-----+---------+-------+------------+
| aid | site_id | count | date       |
+-----+---------+-------+------------+
|   1 |       1 |    45 | 2016-05-10 |
|   2 |       3 |   100 | 2016-05-13 |
|   3 |       1 |   230 | 2016-05-14 |
|   4 |       2 |    10 | 2016-05-14 |
|   5 |       5 |   205 | 2016-05-14 |
+-----+---------+-------+------------+
5 rows in set (0.00 sec)

请注意,在不同的数据库中,BETWEEN 操作符会产生不同的结果!在某些数据库中,BETWEEN 选取介于两个值之间但不包括两个测试值的字段。
在某些数据库中,BETWEEN 选取介于两个值之间且包括两个测试值的字段。
在某些数据库中,BETWEEN 选取介于两个值之间且包括第一个测试值但不包括最后一个测试值的字段。

因此,请检查您的数据库是如何处理 BETWEEN 操作

联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2019

备案号:京ICP备15003423号-3