MySQL查询返回日期时间早于1周的所有记录
要获取早于1周的日期,可以使用以下语法-
select *from yourTableName where yourColumnName < now() - interval 1 week;
为了理解上述概念,让我们创建一个表。创建表的查询如下-
mysql> create table DatesOfOneWeek −> ( −> ArrivalTime datetime −> );
在表中插入一些记录-
mysql> insert into DatesOfOneWeek values(date_add(now(),interval 2 week)); mysql> insert into DatesOfOneWeek values('2018-11-04'); mysql> insert into DatesOfOneWeek values('2018-11-25'); mysql> insert into DatesOfOneWeek values(date_add(now(),interval -1 week)); mysql> insert into DatesOfOneWeek values(date_add(now(),interval 1 week));
让我们检查一下上面插入的记录是否存在。显示表中所有记录的查询如下-
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)
这是MySQL查询,用于获取过去的日期,即1周之前的所有日期-
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)