MySQL多次引用同一个表

时间:2018-01-11 17:56:36

标签: mysql date

我有两张小桌子,我在下面列出了样本数据。它们包含按特定时间范围分组的代表的两种不同类型的销售统计信息。我在配置连接时遇到问题,因此销售代表,时间段和两组数字都排成一行。理想情况下,我希望看到Rep,Date Grouping和两组销售数字都在同一行。目前看来,对于一个代表,一行数据将被吹灭。

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

1 个答案:

答案 0 :(得分:0)

也许这对你有用

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

希望有所帮助