MySQL查询选择今天的最接近日期?
假设当前日期为2019-07-25。现在,我们将看到一个示例并创建一个表,在该表中添加了ShippingDate。
让我们首先创建一个表-
mysql> create table DemoTable667(ShippingDate datetime);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable667 values('2019-01-31'); mysql> insert into DemoTable667 values('2019-07-19'); mysql> insert into DemoTable667 values('2019-07-23'); mysql> insert into DemoTable667 values('2019-08-24');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable667;
这将产生以下输出-
+---------------------+ | ShippingDate | +---------------------+ | 2019-01-31 00:00:00 | | 2019-07-19 00:00:00 | | 2019-07-23 00:00:00 | | 2019-08-24 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
以下是查询以获取距今天最近的日期-
mysql> select ShippingDate from DemoTable667 where date(ShippingDate) = (select min(date(ShippingDate)) from DemoTable667 where date(ShippingDate) > date(now()) );
这将产生以下输出-
+---------------------+ | ShippingDate | +---------------------+ | 2019-07-23 00:00:00 | +---------------------+ 1 row in set (0.00 sec)