我有两个小表格,下面用示例数据进行了概述。它们包含代表销售代表的两种不同类型的销售统计数据,并按特定时间范围进行分组。我在配置联接时遇到麻烦,因此销售代表,时间段和两组数字都排成一行。理想情况下,我想查看销售代表,日期分组以及两组销售数字都在同一行。目前看来,一份代表的所有记录都会散出一行数据。

Table 1 - Closed
Sales Rep Name | Date Group | # Won

Table 2 - Won
Sales Rep Name | Date Group| # Lost


期望的结果

Sales Rep Name | Date Group | # Lost | # Won
Joe Schmo      | June-16    | 25     | 15
Joe Schmo      | July-16    | 38     | 12
Joe Schmo      | Aug-16     | 38     | 12


我现在正按以下方式加入,但似乎没有工作

 Select Sales Rep Name, Date Group, Lost, Won
    From Table 1 - Closed c
    Left Join Table 2 - Won w
    ON c.Sales Rep Name=w.Sales Rep Name
    Left Join Table 2 - Won w
    ON c.Date Group =w.Date Group

最佳答案

也许这应该为您工作

SELECT
closed.date_group,
closed.`sales_rep_name`,
closed.lost,
won.won
FROM
lost
INNER JOIN won ON closed.`sales_rep_name` = closed.`sales_rep_name`
WHERE lost.date_group = won.date_group


希望能帮助到你

关于mysql - MySQL多次引用同一张表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48213196/

10-12 00:51