要从NOW()
-1天获取记录,可以使用以下语法-
select *from yourTableName where yourColumnName >=now()-interval 1 day;
为了理解上述语法,让我们首先创建一个表。查询创建表。
mysql> create table GetRecordsFromNow −> ( −> YourDateTime datetime −> );
现在,在字段中插入一些日期。插入记录的查询如下-
mysql> insert into GetRecordsFromNow values(date_add(now(),interval 3 day)); mysql> insert into GetRecordsFromNow values(date_add(now(),interval -1 day)); mysql> insert into GetRecordsFromNow values(date_add(now(),interval -2 day)); mysql> insert into GetRecordsFromNow values(date_add(now(),interval -3 day)); mysql> insert into GetRecordsFromNow values(date_add(now(),interval -1 day));
在select语句的帮助下显示所有记录。查询如下-
mysql> select *from GetRecordsFromNow;
以下是输出-
+---------------------+ | YourDateTime | +---------------------+ | 2018-12-07 22:30:18 | | 2018-12-03 22:30:31 | | 2018-12-02 22:30:41 | | 2018-12-01 22:30:56 | | 2018-12-03 22:31:04 | +---------------------+ 5 rows in set (0.00 sec)
以下是显示now()
-1天的记录的查询-
mysql> select *from GetRecordsFromNow where YourDateTime >= now()-interval 1 day;
以下是输出-
+---------------------+ | YourDateTime | +---------------------+ | 2018-12-07 22:30:18 | +---------------------+ 1 row in set (0.04 sec)