要获取前一天的记录,以下是语法
select *from yourTableName where date(yourColumnName)= DATE(NOW() - INTERVAL 1 DAY);
为了理解上述语法,让我们创建一个表。创建表的查询如下
mysql> create table yesterDayRecordsDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ArrivalDateTime datetime -> );
使用insert命令在表中插入一些记录。
查询如下
mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2014-11-09 13:45:21'); mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2017-10-19 11:41:31'); mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2019-02-25 10:40:45'); mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2019-02-26 12:06:07'); mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2019-02-25 12:06:47'); mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2019-02-27 11:45:49');
使用select语句显示表中的所有记录
mysql> select *from yesterDayRecordsDemo;
以下是输出
+----+---------------------+ | Id | ArrivalDateTime | +----+---------------------+ | 1 | 2014-11-09 13:45:21 | | 2 | 2017-10-19 11:41:31 | | 3 | 2019-02-25 10:40:45 | | 4 | 2019-02-26 12:06:07 | | 5 | 2019-02-25 12:06:47 | | 6 | 2019-02-27 11:45:49 | +----+---------------------+ 6 rows in set (0.00 sec)
这是从昨天获取MySQL记录的查询
mysql> select *from yesterDayRecordsDemo where date(ArrivalDateTime)= DATE(NOW() - INTERVAL 1 DAY);
以下是输出
+----+---------------------+ | Id | ArrivalDateTime | +----+---------------------+ | 4 | 2019-02-26 12:06:07 | +----+---------------------+ 1 row in set (0.08 sec)