简体   繁体   中英

MySQL adding more than one WHERE with sub-select query

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
select day, sum(diff) as total_diff
from (
select t1.sub_meterID, date(t1.`date`) as day, max(t1.value) - min(t1.value) as diff
from `sub_meter_data` t1
JOIN othertable t2 ON t1.sub_meterID=t2.meterID
JOIN moretable t3 ON t2.ID=t3.meterID
where date(t1.`date`) > '2012-10-01'
   and t1.sub_meterID in ('58984','58985','58986','58987')
group by t1.sub_meterID, date(t1.`date`)
) a
group by day

Read more info about MySQL Joins here

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM