返回所有时间戳早于 1 周记录的 MySQL 查询
要获取早于 1 周的日期,可以使用以下语法 −
select *from yourTableName where yourColumnName < now() - interval 1 week;
为了理解上述概念,我们创建一个表。创建表的查询如下 −
mysql> create table DatesOfOneWeek −> ( −> ArrivalTime datetime −> ); Query OK, 0 rows affected (0.87 sec)
在表中插入一些记录 −
mysql> insert into DatesOfOneWeek values(date_add(now(),interval 2 week)); Query OK, 1 row affected (0.11 sec) mysql> insert into DatesOfOneWeek values('2018-11-04'); Query OK, 1 row affected (0.14 sec) mysql> insert into DatesOfOneWeek values('2018-11-25'); Query OK, 1 row affected (0.11 sec) mysql> insert into DatesOfOneWeek values(date_add(now(),interval -1 week)); Query OK, 1 row affected (0.14 sec) mysql> insert into DatesOfOneWeek values(date_add(now(),interval 1 week)); Query OK, 1 row affected (0.11 sec)
我们检查上述插入的记录是否存在。显示表中所有记录的查询如下 −
mysql> select *from DatesOfOneWeek;
输出如下 −
+---------------------+ | ArrivalTime | +---------------------+ | 2018-12-20 18:11:02 | | 2018-11-04 00:00:00 | | 2018-11-25 00:00:00 | | 2018-11-29 18:11:40 | | 2018-12-13 18:11:46 | +---------------------+ 5 rows in set (0.00 sec)
以下是获取过去日期(即 1 周前的所有日期)的 MySQL 查询 −
mysql> select *from DatesOfOneWeek where ArrivalTime < now() - interval 1 week;
输出如下 −
+---------------------+ | ArrivalTime | +---------------------+ | 2018-11-04 00:00:00 | | 2018-11-25 00:00:00 | | 2018-11-29 18:11:40 | +---------------------+ 3 rows in set (0.00 sec)
Advertisement