MySQL 日期比较以获取给定范围之间的日期?
mysqlmysqli database
首先我们创建一个表 −
mysql> create table DemoTable ( AdmissionDate date ); Query OK, 0 rows affected (0.73 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('2019-08-31'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-09-01'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-05-10'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2019-06-12'); Query OK, 1 row affected (0.25 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+---------------+ | AdmissionDate | +---------------+ | 2019-08-31 | | 2019-09-01 | | 2019-05-10 | | 2019-06-12 | +---------------+ 4 rows in set (0.00 sec)
以下是执行日期比较的查询,以获取给定范围内的日期 −
mysql> select *from DemoTable where AdmissionDate >='2019-06-01' and AdmissionDate <='2019-08-31';
这将产生以下输出 −
+---------------+ | AdmissionDate | +---------------+ | 2019-08-31 | | 2019-06-12 | +---------------+ 2 rows in set (0.00 sec)