Hi i currently have two tables, one has daily entries and other gets the total of those and stores monthly values.
I'm able to get totals of each month and year with below query, but what i'd like to do is only get the months where month is greater than and equal to current month. btw date column is in date format (yyyy-mm-dd)
$revbyMonth = $conn->prepare("SELECT EXTRACT(MONTH FROM date) as month, EXTRACT(YEAR FROM date) as year, SUM(rev) as total FROM {$tempName} GROUP BY year, month");
You want to add a where
clause immediately after the from
clause. I think this will work for you. If the date has no time component:
where date > date_sub(curdate(), interval day(curdate()) day)
The expression date_sub(curdate(), interval day(curdate()) day)
gets the last day of the previous month. If the dates have a time component:
where date >= date_sub(curdate(), interval day(curdate()) - 1 day)
should work.
Note: this is better than other methods that process date
using functions, such as:
where year(date) > year(curdate()) or
(year(date) = year(curdate()) and month(date) >= month(curdate()) )
The use of the functions prevents MySQL from using an index on the date
column.