mysql between and实例及边界值分析

时间:2016-09-03
mysql BETWEEN AND关键字可以判断某个字段的值是否在指定的范围内,范围是包含边界值的,本文章向大家介绍between and的使用实例和需要注意的地方,需要的朋友可以参考一下。

mysql中可以使用between and来限定一个数据的范围,between and关键字其实相当于“大于等于和小于等于”的条件。请看下面实例:

现有一employee_tbl表,其数据结果如下所示:

mysql> SELECT * FROM employee_tbl;
+------+------+------------+--------------------+
| id   | name | work_date  | daily_typing_pages |
+------+------+------------+--------------------+
|    1 | John | 2007-01-24 |                250 |
|    2 | Ram  | 2007-05-27 |                220 |
|    3 | Jack | 2007-05-06 |                170 |
|    3 | Jack | 2007-04-06 |                100 |
|    4 | Jill | 2007-04-06 |                220 |
|    5 | Zara | 2007-06-06 |                300 |
|    5 | Zara | 2007-02-06 |                350 |
+------+------+------------+--------------------+

现在我们要获取字段daily_typing_pages值在170到300之间的数据,可以使用between and来实现:

mysql> SELECT * FROM employee_tbl 
    -> WHERE daily_typing_pages BETWEEN 170 AND 300; 
/*  http://www.manongjc.com/article/1445.html */
+------+------+------------+--------------------+
| id   | name | work_date  | daily_typing_pages |
+------+------+------------+--------------------+
|    1 | John | 2007-01-24 |                250 |
|    2 | Ram  | 2007-05-27 |                220 |
|    3 | Jack | 2007-05-06 |                170 |
|    4 | Jill | 2007-04-06 |                220 |
|    5 | Zara | 2007-06-06 |                300 |
+------+------+------------+--------------------+

由查询结果可以看出,between and关键字范围是包含边界值的(注意not  between and的范围是不包含边界值),因此也可以使用"大于等于和小于等于"来实现:

mysql>SELECT * FROM employee_tbl 
    ->WHERE daily_typing_pages >= 170 AND
    ->daily_typing_pages <= 300;
+------+------+------------+--------------------+
| id   | name | work_date  | daily_typing_pages |
+------+------+------------+--------------------+
|    1 | John | 2007-01-24 |                250 |
|    2 | Ram  | 2007-05-27 |                220 |
|    3 | Jack | 2007-05-06 |                170 |
|    4 | Jill | 2007-04-06 |                220 |
|    5 | Zara | 2007-06-06 |                300 |
+------+------+------------+--------------------+

上一页 下一页

原文地址:http://www.manongjc.com/article/1445.html