MySQL BETWEEN 子句

BETWEEN 子句

您可以使用BETWEEN子句来替换“大于等于 and 小于等于”条件的组合。要了解BETWEEN子句,请考虑一个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         |
+------+------+------------+--------------------+
7 rows in set (0.00 sec)

 现在,假设基于上表,您要获取行进日大于170且等于和小于300且等于的条件的记录。可以使用 >=和 <= 条件,如下所示-

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         |
+------+------+------------+--------------------+
5 rows in set (0.03 sec)

使用BETWEEN子句可以实现以下相同效果

mysql> SELECT * FROM employee_tbl 
   -> WHERE daily_typing_pages BETWEEN 170 AND 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         |
+------+------+------------+--------------------+
5 rows in set (0.03 sec)  

 相关资料

MySQL GROUP BY 子句

MySQL COUNT 函数

MySQL CONCAT 函数

 

MySQL BETWEEN 子句

上一篇:[db2数据库实时同步]DTCC 干货 | 中国银联跨中心,异构数据同步技术与实践


下一篇:MongoDB分?(Sharding)技术