从MySQL中过去7天的记录中获取datetime行
为此,您可以使用INTERVAL7天概念。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, AdmissionDate datetime );
注意-假设当前日期为2019-08-23。
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(AdmissionDate) values('2019-01-23'); mysql> insert into DemoTable(AdmissionDate) values('2019-08-15'); mysql> insert into DemoTable(AdmissionDate) values('2019-08-16'); mysql> insert into DemoTable(AdmissionDate) values('2019-08-24');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+---------------------+ | Id | AdmissionDate | +----+---------------------+ | 1 | 2019-01-23 00:00:00 | | 2 | 2019-08-15 00:00:00 | | 3 | 2019-08-16 00:00:00 | | 4 | 2019-08-24 00:00:00 | +----+---------------------+ 4 rows in set (0.00 sec)
这是从过去7天的确切记录中获取DateTime行的查询-
mysql> select *from DemoTable where date(AdmissionDate)=CURDATE() - interval 7 day;
这将产生以下输出-
+----+---------------------+ | Id | AdmissionDate | +----+---------------------+ | 3 | 2019-08-16 00:00:00 | +----+---------------------+ 1 row in set (0.04 sec)