在 8 个月间隔后获取记录的 MySQL 查询
为此,请使用 INTERVAL 8 MONTH 并从当前日期获取 8 个月内的记录 −
select *from yourTableName where yourColumnName>= (CURRENT_DATE() - INTERVAL 8 MONTH);
注意 − 假设当前日期为 2018-02-06
首先,让我们创建一个表 −
mysql> create table DemoTable (StudentName varchar(100), AdmissionDate date); Query OK, 0 rows affected (0.75 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('John','2019-01-21'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('Chris','2019-10-04'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('Robert','2018-02-01'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David','2019-07-07'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-------------+---------------+ | StudentName | AdmissionDate | +-------------+---------------+ | John | 2019-01-21 | | Chris | 2019-10-04 | | Robert | 2018-02-01 | | David | 2019-07-07 | +-------------+---------------+ 4 rows in set (0.00 sec)
以下是获取 8 个月间隔后记录的查询 −
mysql> select *from DemoTable where AdmissionDate >= (CURRENT_DATE() - INTERVAL 8 MONTH);
这将产生以下输出 −
+-------------+---------------+ | StudentName | AdmissionDate | +-------------+---------------+ | John | 2019-01-21 | | Chris | 2019-10-04 | | David | 2019-07-07 | +-------------+---------------+ 3 rows in set (0.00 sec)
广告