在 MySQL 中显示从当前日期到同一个月剩余时间的记录?
mysqlmysqli database
首先我们创建一个表 −
mysql> create table DemoTable ( BookingDate date ); Query OK, 0 rows affected (0.67 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('2019-09-21'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2018-09-10'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('2019-09-10'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-09-08'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2016-09-18'); Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-------------+ | BookingDate | +-------------+ | 2019-09-21 | | 2018-09-10 | | 2019-09-10 | | 2019-09-08 | | 2016-09-18 | +-------------+ 5 rows in set (0.00 sec)
以下是 MySQL 中从当前日期到本月剩余时间的查询 −
mysql> select *from DemoTable where BookingDate between curdate() and last_day(curdate());
这将产生以下输出 −
+-------------+ | BookingDate | +-------------+ | 2019-09-21 | | 2019-09-10 | | 2019-09-08 | +-------------+ 3 rows in set (0.01 sec)