为此,您可以使用DATEDIFF()函数。当前日期时间如下-
select now(); +---------------------+ | now() | +---------------------+ | 2019-06-09 19:15:56 | +---------------------+ 1 row in set (0.00 sec)
让我们首先创建一个表-
create table DemoTable -> ( -> ShippingDate datetime -> );
使用插入命令在表中插入一些记录-
insert into DemoTable values('2019-06-01'); insert into DemoTable values('2019-06-02'); insert into DemoTable values('2019-06-14'); insert into DemoTable values('2019-05-21');
使用select语句显示表中的所有记录-
select *from DemoTable;
输出结果
+---------------------+ | ShippingDate | +---------------------+ | 2019-06-01 00:00:00 | | 2019-06-02 00:00:00 | | 2019-06-14 00:00:00 | | 2019-05-21 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
以下是选择超过一周的行的查询。假设当前日期为“ 2019-06-09”。因此,将选择超过一周的行,即“ 2019-06-02”之前-
select *from DemoTable where DATEDIFF(now(),ShippingDate) > 7;
输出结果
+---------------------+ | ShippingDate | +---------------------+ | 2019-06-01 00:00:00 | | 2019-05-21 00:00:00 | +---------------------+ 2 rows in set (0.00 sec)