This code gives me a nice result. But I want to add in some fields from another table, I’m not sure where to put the additioanal WHERE code…
select day, sum(diff) as total_diff
from (
select sub_meterID, date(`date`) as day, max(value) - min(value) as diff
from `sub_meter_data`
where date(`date`) > '2012-10-01'
and sub_meterID in ('58984','58985','58986','58987')
group by sub_meterID, date(`date`)
) a
group by day
The additional fields I need to match up are:
othertable.meterID = sub_meter_data.sub_meterID
moretable.meterID = othertableID
Read more info about MySQL Joins here