从 MySQL NOW() -1 Day 中选择记录?
mysqlmysqli database
要从 NOW()-1 Day 获取记录,您可以使用以下语法 −
select *from yourTableName where yourColumnName >=now()-interval 1 day;
为了理解上述语法,让我们首先创建一个表. The query to create a table.
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)