Sum values by year with MySQL/PHP
I've got a minor problem with a database admin page that I use to update values and totals in a MySQL database for generating various figures and data for a website.
A bit of background - the database table in question holds fundraising totals for a charity bookstall, with the table having three columns - id, date, and amountraised. Summing the total raised for a specific year or the current year isn't a problem - I've already got a running total from last February when the fundraising started and one for the current year, and that's working fine. I've just realised though while adding in this week's total raised that while I know the total of all funds raised, and how much has been raised so far this year, that nowhere is the total for last year shown. Obviously for this year I can just take the total to date for this year off the grand total, but I really need a total for each year to be shown.
I have a table displayed on the page that shows the fundraising targets for each year, and I want to add in the year total to that table so that over time we can see both the target for each year and amount raised that year.
In the bit of the page that displays the running total, I have the following, which display the grand total, and the total for the current year:
$result = mysql_fetch_array($query);
$sumtotal = $result["sum_total"];
$query2 = mysql_query("SELECT SUM(amountraised) AS year_total FROM fundraisingtotal WHERE YEAR(开发者_如何学运维date
) = YEAR(CURDATE());");
<p class="dbpara">The current fundraising total since February 2009 is <b>£<? echo "$sumtotal"; ?></b>.</p>
<p class="dbpara">The current fundraising total for this year (<?php echo date('Y'); ?>) is <b>£<? echo "$yeartotal"; ?></b>.</p>
Also on the same page I have the display of the fundraising targets, and it's this bit I've got the problem with, partly because the fundraising totals data's in a separate table to the annual targets (annual targets are in a table with just the year and the target), and partly because the table that displays the annual target uses a while loop to write out the data from the database to the page. This is the code I'm using to write out the table and the data (I've put the table cell for the total raised in, but obviously no code for it):
<?php
$query="SELECT * FROM annualtarget ORDER BY year DESC";
$result=mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$year = mysql_result($result,$i,"year");
$target = mysql_result($result,$i,"target");
$id = mysql_result($result,$i,"id");
echo "<tr><td class=\"yr\"> $year </td><td class=\"tg\"> £$target </td><td class=\"total\"> ???? </td><td class=\"update\"><a href=\"updatetarget.php?id=$id\">Update</a></td><td class=\"delete\"><a href=\"delete.php?id=$id\">Delete</a></td></tr>";
++$i; } } else { echo "<tr><td colspan=\"4\">The database is empty!</td></tr>"; }
mysql_close();
?>
Obviously because I'll need a different query to calculate the totals for the year, but I can't work out how I can work that into the while loop. I could use the date from the fundraising amount table by separating out the year bit of the date, but as the target for each year is in a different table that wouldn't work, and I really don't want to have to hard-code the year and its total raised in the database just to display, when I can (if I can figure out how to do it) use the database to calculate it.
I'm probably missing something ridiculously obvious here, but I can't figure it out. Has anyone any idea how I can do this?
Use a "group by" query to get all your totals at once.
SELECT YEAR(date) as year, SUM(amountraised) AS year_total
FROM fundraisingtotal
GROUP BY YEAR(date)
SELECT a.year , SUM( f.amountraised ) , a.target
FROM annualtarget a INNER JOIN fundraisingtotal f ON (a.year = YEAR( f.date ))
GROUP BY a.year
精彩评论