0

这段代码给了我一个很好的结果。但是我想从另一个表中添加一些字段,我不确定在哪里放置额外的 WHERE 代码......

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

我需要匹配的其他字段是:

othertable.meterID = sub_meter_data.sub_meterID

moretable.meterID = othertableID
4

1 回答 1

1
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

在此处阅读有关 MySQL 连接的更多信息

于 2012-10-19T22:05:26.397 回答