MySQL query to get records after an interval of 8 months

MySQLMySQLi Database

For this, use INTERVAL 8 MONTH and fetch records 8 months from the current date −

select *from yourTableName where yourColumnName>= (CURRENT_DATE() - INTERVAL 8 MONTH);

Note − Let’s say the current date is: 2018-02-06

Let us first create a table −

mysql> create table DemoTable (StudentName varchar(100), AdmissionDate date);
Query OK, 0 rows affected (0.75 sec)

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------+---------------+
| 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)

Following is the query to get records after an interval of 8 months −

mysql> select *from DemoTable where AdmissionDate >= (CURRENT_DATE() - INTERVAL 8 MONTH);

This will produce the following output −

+-------------+---------------+
| StudentName | AdmissionDate |
+-------------+---------------+
| John        | 2019-01-21    |
| Chris       | 2019-10-04    |
| David       | 2019-07-07    |
+-------------+---------------+
3 rows in set (0.00 sec)
raja
Updated on 22-Aug-2019 07:06:51

Advertisements