MySQL将两个值相加不同的表

时间:2016-11-10 17:35:57

标签: mysql

首先,对不起,因为您之前可能已经看过这个问题,但我已经尝试过这里的解决方案了。但它不符合我的需要。 我有两张桌子。其中一个用于销售部门代理,另一个用于技术支持代理。 我需要每天在两个部分中汇总已接来电的值。

我的表格结构和样本数据:

enter image description here [enter image description here

示例:日期列将保持不变,已接收列= sales.received + tech.received,sales.answered + tech.answered 我找到了这个:

SELECT   
    (SELECT SUM(`Received_Calls`) FROM sales) + (SELECT SUM(`Received_Calls`) FROM tech
    ) FROM DUAL

但它只显示了总数...而没有显示每天的每日计算。

预期结果:

---------------------------------------------
Date       | Received | Answered | Abandoned
---------------------------------------------
|2014-11-14|   8406   |   8363   |    43
|2014-11-15|   9909   |   9792   |    116
---------------------------------------------

并且没有其他日期的桌子上没有可用的日期,两个表格上的每日日期都可用,没有例外。

任何帮助?谢谢:))

1 个答案:

答案 0 :(得分:3)

假设日期是唯一的(每个表),您可以根据它加入并只添加适当的值:

SELECT s.`date`, 
       s.received_calls + t.received_calls AS received,
       s.answered_calls + t.answered_calls AS answered,
       s.abandoned_calls + t.abandoned_calls AS abandoned
FROM   sales s
JOIN   tech t ON s.`date` = t.`date`