I have a MYSQL table structured like this:
1st column: id;
2nd column: date;
3rd column: integer value;
The date can vary from 2010-01-01 to 2014-12-20 for example.
In the PHP code i have defined two variables in "Ymd" format
, let's say the values are:
$first_date = "2011-10-17"
$last_date = "2014-11-04"
How can I get the sum of the values from the 3rd column for each month in the interval defined by the $first_date
and $last_date
?
You would do a SQL query like this:
select date_format(date, '%Y-%m') as yyyymm, sum(value)
from table t
where date between $first_date and $second_date
group by date_format(date, '%Y-%m')
order by yyyymm;
I would recommend that you parameterize the query rather than putting the variables directly into the SQL string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.