MySQL-选择的日期比今天早一周吗?
要获取从今天开始提前一周的日期,请使用DATEDIFF。我们首先获取当前日期-
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2019-12-20 | +------------+ 1 row in set (0.00 sec)
我们将首先创建一个表-
mysql> create table DemoTable1990 ( ShippingDate date );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1990 values('2019-12-13'); mysql> insert into DemoTable1990 values('2019-12-21'); mysql> insert into DemoTable1990 values('2019-12-20'); mysql> insert into DemoTable1990 values('2019-12-24');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1990;
这将产生以下输出-
+--------------+ | ShippingDate | +--------------+ | 2019-12-13 | | 2019-12-21 | | 2019-12-20 | | 2019-12-24 | +--------------+ 4 rows in set (0.00 sec)
这是查询以选择从今天起提前一周的日期-
mysql> select * from DemoTable1990 where datediff(curdate(),ShippingDate)%7=0;
这将产生以下输出-
+--------------+ | ShippingDate | +--------------+ | 2019-12-13 | | 2019-12-20 | +--------------+ 2 rows in set (0.04 sec)