Community Service Table
|student name (id in real table)|hours|year|event name (id in real table)|
|Johnny Smith | 5|2010|Beach Clean-up |
|Samantha Bee | 3|2011|Daily Show Volunteering |
|Samantha Bee | 2|2011|Daily Show Bake Sale |
|Bilbo Baggins | 10|2011|The Shire Feast Setup |
From this table I'd like the following output:
Hours in 2011:
Johnny Smith: 0 (his hours are in 2010)
Samantha Bee: 5
Bilbo Baggins: 10
Here's my failed attempt:
mysql_query("select name,
sum(hours)
from community_service
where year = '2011'
or year is null
group by name");
I understand why 'year is null' doesn't give me what I want, I just don't know how to do it correctly.
Thanks in advance!
Since my simplied query seems to be confusing people, here's the whole enchilada:
SELECT DISTINCT contacts.contactID,
sum(hours) as hours,
contacts.first,
contacts.last,
contacts.graduates
from contacts
left join comm_stud using (contactID)
left join comm_svc using (csID)
left join tbl_yeardiv on comm_svc.termID = tbl_yeardiv.yeardivID
WHERE (year = '2010-11')
group by contacts.contactID
tbl_yeardiv contains school terms like Fall and Spring semesters. comm_stud joins students to each comm_svc event.