Santosh Khatri Santosh Khatri - 1 month ago 5
PHP Question

How do I filter the rows using the datediff function in MySQL?

I have a table with following structure:

+===========+============+
| titleName | date |
+===========+============+
| title1 | 2016-08-06 |
+===========+============+
| title2 | 2016-08-03 |
+===========+============+
| title3 | 2016-07-29 |
+===========+============+
| title4 | 2016-08-09 |
+===========+============+
| title5 | 2016-09-03 |
+===========+============+


I want to select only the rows that are not older than 90 days from the current date (2016-11-04) as follows:

+===========+============+
| titleName | date |
+===========+============+
| title4 | 2016-08-09 |
+===========+============+
| title5 | 2016-09-03 |
+===========+============+


I have tried the following query, but it doesn't work as expected:

SELECT titleName, Datediff(NOW(), publish_date) AS datediff FROM myTable ORDER BY titleName DESC LIMIT 8


Please help me to make the correct query.

Answer

Use the WHERE clause with an INTERVAL:

SELECT * FROM the_table WHERE `date` + INTERVAL 90 DAY >= NOW();
Comments