SQL连接2个表,其中包含一些重叠值

时间:2013-09-29 11:43:11

标签: mysql sql join

我正在尝试从以下2个表中创建一个表并遇到很多问题。

表1包含以下内容:

Month,
Salesperson_Assigned,
Product1_Revenue

表2具有以下(不同产品):

Month,
Salesperson_Assigned,
Product2_Revenue

问题是虽然所有月份都相同,但表1中有一些销售人员不在表2中,反之亦然。显然收入是不同的。我想加入表格看起来像Month,Salesperson,Product1_revenue + Product2_revenue。

这是我正在使用的查询:

SELECT
Table1.month,
Table1.salesperson_assigned,
sum(Table1.revenue + Table2.revenue)

FROM
Table1
INNER JOIN Table 2 ON (Table1.month = Table2.month) AND (Table1.salesperson_assigned = Table2.salesperson_assigned)

即使我确认每个表中的收入值都是正确的,但输出仍然不正确。输出似乎为销售人员构成了额外的值,这些销售人员只出现在1个表中,该字段应为null。

任何人都对如何正确加入2个表有任何建议,因此它的作用类似于数据透视表,将唯一值添加到“salesperson assigned”列并为两列添加收入,但当销售人员只在一张表让它尊重收入零值?

2 个答案:

答案 0 :(得分:1)

你是否尝试过这种方式:

 SELECT
    Table1.month,
    Table1.salesperson_assigned,
    sum(Table1.revenue + Table2.revenue)

    FROM
    Table1 FULL OUTER JOIN Table2 on (Table1.month = Table2.month AND Table1.salesperson_assigned = Table2.salesperson_assigned)

答案 1 :(得分:1)

理想情况下,对于这些类型的情况FULL OUTER JOINS很有用 - 但显然MySQL不支持FULL OUTER JOINS请参阅:Full Outer Join in MySQL

更新(不使用右外连接):

create table table1
(month int
,salesperson_assigned int
,Product1_revenue int);

create table table2
(month int
,salesperson_assigned int
,Product2_revenue int);


insert into table1 values(1,10,100);
insert into table1 values(2,10,200);
insert into table1 values(1,11,40);
insert into table1 values(2,11,800);
insert into table1 values(3,11,400);


insert into table2 values(1,10,100);
insert into table2 values(2,10,200);
insert into table2 values(1,12,40);
insert into table2 values(2,12,200);

select 
table1.month
,table1.salesperson_assigned
,ifnull(table1.Product1_revenue,0) as Product1_revenue
,ifnull(table2.Product2_revenue,0) as Product2_revenue
,ifnull(table1.Product1_revenue,0)+ifnull(table2.Product2_revenue,0) as total_revenue
from table1
left outer join table2
on table1.salesperson_assigned=table2.salesperson_assigned and table1.month=table2.month
union 
select 
table2.month
,table2.salesperson_assigned
,ifnull(table1.Product1_revenue,0) as Product1_revenue
,ifnull(table2.Product2_revenue,0) as Product2_revenue
,ifnull(table1.Product1_revenue,0)+ifnull(table2.Product2_revenue,0) as total_revenue
from table2
left outer join table1
on table2.salesperson_assigned=table1.salesperson_assigned  and table1.month=table2.month
order by 2,1;

返回:

Month   salesperson_assigned    Product1_revenue    Product2_revenue    total_revenue
1       10                          100                 100                 200
2       10                          200                 200                 400
1       11                          40                  0                   40
2       11                          800                 0                   800
3       11                          400                 0                   400
1       12                          0                   40                  40
2       12                          0                   200                 200

ps:请考虑发布代码以重现您的数据,以便您可以更快地做出回应。