MySQL 日期比较以获取给定范围内的日期?
我们首先创建一个表 −
mysql> create table DemoTable ( AdmissionDate date ); Query OK, 0 rows affected (0.73 sec)
使用插入命令向表中插入一些记录 −
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)
广告