如何对mysql中的多个不同表求和

如何对mysql中的多个不同表求和,mysql,Mysql,我有三个问题 select uid, sum(call_duration) as cTime from privacy_call_history where event_date >= '2015-11-20 01:00:00' and event_date < '2015-11-20 01:00:00' + interval 1 hour group by uid having sum(call_duration) > 0; select uid, sum(run_tim

我有三个问题

select uid, sum(call_duration) as cTime from privacy_call_history 
where event_date >= '2015-11-20 01:00:00' and event_date < '2015-11-20 01:00:00' + interval 1 hour
group by uid having sum(call_duration) > 0;

select uid, sum(run_time) as rTime from app_finish_history
where event_date >= '2015-11-20 01:00:00' and event_date < '2015-11-20 01:00:00' + interval 1 hour
group by uid having sum(run_time) > 0;

select uid, sum(chat_time) as wcTime, sum(msg_avg_time) as maTime from whisper_chat_history
where event_date >= '2015-11-20 01:00:00' and event_date < '2015-11-20 01:00:00' + interval 1 hour
group by uid having sum(chat_time) > 0 and sum(msg_avg_time) > 0;

可以将查询作为子查询组合到单个结果集中。但是,您要么需要完整的UID列表来自的用户表,要么需要在mysql中模拟完整的外部联接。我会使用用户表或其他任何名称

select user.uid, ifnull(t1.cTime,0) cTime, ifnull(t2.rTime,0) rTime, ifnull(t3.wcTime,0) wcTime, ifnull(t3.maTime,0) maTime
from user left join
(select uid, sum(call_duration) as cTime from privacy_call_history 
where event_date >= '2015-11-20 01:00:00' and event_date < '2015-11-20 01:00:00' + interval 1 hour
group by uid having sum(call_duration) > 0) t1 on user.uid=t1.uid
left join
(select uid, sum(run_time) as rTime from app_finish_history
where event_date >= '2015-11-20 01:00:00' and event_date < '2015-11-20 01:00:00' + interval 1 hour
group by uid having sum(run_time) > 0) t2 on user.uid=t2.uid
left join
(select uid, sum(chat_time) as wcTime, sum(msg_avg_time) as maTime from whisper_chat_history
where event_date >= '2015-11-20 01:00:00' and event_date < '2015-11-20 01:00:00' + interval 1 hour
group by uid having sum(chat_time) > 0 and sum(msg_avg_time) > 0) t3 on user.uid=t3.uid

如果要筛选出未出现在任何子选项中的用户,请添加一个where citeria,该用户至少需要1个非空值:coalescecTime、rTime、wcTime、maTime not null

也许您可以在我的答案旁边打勾,然后: