Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concisely finding rows from less than 6 months ago in MySQL

Tags:

date

mysql

I'm trying to find entries in my table from between 6 months ago and today. Here's the query that I'm currently running:

WHERE (DATE(l.date_fin) BETWEEN DATE(DATE_ADD(NOW(), INTERVAL -6 MONTH)) AND CURDATE())

However, it doesn't look right to me: it feels like there's a more concise and/or faster way to get the same results as this query. Is there any such way?

like image 501
prcaen Avatar asked Dec 05 '22 15:12

prcaen


2 Answers

This should be enough to get what you want:

WHERE l.date_fin > CURRENT_DATE() - INTERVAL 6 MONTH;
like image 50
Puggan Se Avatar answered Dec 10 '22 09:12

Puggan Se


How about something like this?

SELECT l.date_fin FROM table WHERE TIMESTAMPDIFF(MONTH,l.date_fin,NOW()) < 6
like image 29
NewInTheBusiness Avatar answered Dec 10 '22 10:12

NewInTheBusiness