Reputation: 11
Hello everyone I hope you can help me on this
I have a table m_membership_list
and there are fields date_from
and date_to
and I need to count the number of the records where current date is between date_to
and the last_30 days of the the date_to
.
I searched about this but I did not find the exact example but I think the logic is like in MySQL Query - Records between Today and Last 30 Days
But still I can't get it.
Here is my SQL.
SELECT * FROM m_membership_list WHERE NOW() IN DATE_SUB(date_to, INTERVAL 30 DAY)
I think the logic is almost like this but i dont know why it is not working.
SELECT * FROM m_membership_list WHERE NOW() BETWEEN date_to AND (DATE_SUB(date_to,INTERVAL 30 DAY))
Improv
Upvotes: 0
Views: 254
Reputation: 51
try this
SELECT count(*)
FROM m_membership_list
WHERE TO_DAYS( NOW( ) ) - TO_DAYS( date_to) <=30
Upvotes: 0
Reputation: 18600
Try this
SELECT *
FROM m_membership_list
WHERE NOW() BETWEEN NOW()-INTERVAL 30 DAY AND date_to;
Upvotes: 0
Reputation: 125835
As documented under expr BETWEEN min AND max
:
If
expr
is greater than or equal tomin
andexpr
is less than or equal tomax
,BETWEEN
returns1
, otherwise it returns0
.
Since NOW()
cannot simultaneously be greater than or equal to date_to
and less than or equal to DATE_SUB(date_to,INTERVAL 30 DAY)
, your clause is necessarily always false. Reverse the operands:
WHERE NOW() BETWEEN DATE_SUB(date_to,INTERVAL 30 DAY) AND date_to
Upvotes: 1