从 MySQL NOW() -1 天中选择记录?
要获取 NOW()-1 天的记录,可以使用以下语法 -
select *from yourTableName where yourColumnName >=now()-interval 1 day;
要了解上述语法,让我们首先创建一个表。创建表的查询。
mysql> create table GetRecordsFromNow −> ( −> YourDateTime datetime −> ); Query OK, 0 rows affected (1.76 sec)
现在将一些日期插入字段。插入记录的查询如下 -
mysql> insert into GetRecordsFromNow values(date_add(now(),interval 3 day)); Query OK, 1 row affected (0.28 sec) mysql> insert into GetRecordsFromNow values(date_add(now(),interval -1 day)); Query OK, 1 row affected (0.11 sec) mysql> insert into GetRecordsFromNow values(date_add(now(),interval -2 day)); Query OK, 1 row affected (0.14 sec) mysql> insert into GetRecordsFromNow values(date_add(now(),interval -3 day)); Query OK, 1 row affected (0.09 sec) mysql> insert into GetRecordsFromNow values(date_add(now(),interval -1 day)); Query OK, 1 row affected (0.08 sec)
在 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)
以下是显示现在() -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)
广告