I want to make a comparison if the date is within 30 days.
For example, I have the date 10/01/2016 and I want it to return all the results with dates 30 days close to that for example, 09/02/2016.
I want to make a comparison if the date is within 30 days.
For example, I have the date 10/01/2016 and I want it to return all the results with dates 30 days close to that for example, 09/02/2016.
I usually do it this way below. Use NOW()
to get the current date and subtract within 30 days using INTERVAL 30 DAY
. In the query I make the comparison if the creation date ( created_at
of type timestamp
) is larger, then return all data:
SELECT * FROM 'task' WHERE created_at > (NOW() - INTERVAL 30 DAY)
So all results will be within 30 days.
You can use the expression;
SELECT * FROM 'table'
where STR_TO_DATE(data, "%d/%m/%Y") >= STR_TO_DATE('10/01/2016', "%d/%m/%Y")
and STR_TO_DATE(data, "%d/%m/%Y") <= STR_TO_DATE(09/02/2016', "%d/%m/%Y");
Or even use between
:
SELECT * FROM 'table'
where STR_TO_DATE(data, "%d/%m/%Y") between STR_TO_DATE('10/01/2016', "%d/%m/%Y")
and STR_TO_DATE(09/02/2016', "%d/%m/%Y");
I was able to solve this problem
SELECT * FROM PESSOAS WHERE DATEDIFF(STR_TO_DATE(ANIVERSARIO, "%d/%m/%Y"),STR_TO_DATE('10/01/2013', "%d/%m/%Y")) BETWEEN -30 and 30