加入具有多个日期的多个表

Joining multiple tables with multiple dates

我有个师傅table就是这样

customer_id  Customer_name
1             A
2             B
3             C
4             D

而且我有多个用户 activity table。我只给出了 2 个这样的 tables 的例子,但是有 20 个这样的 tables

Table 1:

customer_id     activity_date   No_of_likes
2                1/1/16          2
3                1/1/16          3
2                2/1/16          5
2                1/1/16          1

Table 2:

customer_id     activity_date   No_of_comments
2                1/1/16          1
4                5/1/16          7
2                6/1/16          2

这是我需要的最终 table。我没有主日历 table,我不确定如何加入以获得以下输出

customer_id     activity_name   activity_date    quantity
1                -                  -              -
2               Likes             1/1/16            3
2               Likes             2/1/16            5
2               Comments          1/1/16            1
2               Comments          6/1/16            2
3               Likes             1/1/16            3
4               Comments          5/1/16            7

而且我也需要这样的输出。我认为 coalesce 解决了上述问题,但我不确定如何执行以下操作

  customer_id      activity_date    like_quantity    comment_activity 
     1                 -                -                     - 
     2                1/1/16            3                     1 
     2                2/1/16            5                     - 
     2                6/1/16            -                     2 
     3                1/1/16            3                     - 
     4                5/1/16            -                     7

希望我说清楚了。请帮我 SQL 查询加入

你可以试试这个:

使用 UNIONLEFT JOIN 你可以做到这一点。

SELECT 
  your_master_table.customer_id,
  COALESCE(t.activity_name,'-') AS activity_name,
  COALESCE(t.activity_date,'-') AS activity_date,
  COALESCE(t.quantity,'-') AS quantity
FROM your_master_table

LEFT JOIN 
(
    SELECT 
    customer_id,
    activity_date,
    No_of_likes AS quantity,
    'Likes' AS activity_name
    FROM TABLE_1

    UNION 

    SELECT 
    customer_id,
    activity_date,
    No_of_comments AS quantity,
    'Comments' AS activity_name
    FROM TABLE_2

) AS t
ON your_master_table.customer_id = t.customer_id
ORDER BY your_master_table.customer_id;

SQL FIDDLE DEMO

运行 对您的示例输入进行上述查询,您将获得以下输出:

输出:

| customer_id | activity_name | activity_date | quantity |
|-------------|---------------|---------------|----------|
|           1 |             - |             - |        - |
|           2 |      Comments |    2016-01-01 |        1 |
|           2 |      Comments |    2016-01-06 |        2 |
|           2 |         Likes |    2016-01-01 |        2 |
|           2 |         Likes |    2016-01-02 |        5 |
|           2 |         Likes |    2016-01-01 |        1 |
|           3 |         Likes |    2016-01-01 |        3 |
|           4 |      Comments |    2016-01-05 |        7 |

编辑:

根据您的需求变化

SELECT 
your_master_table.customer_id,
COALESCE(DATE_FORMAT(t.activity_date,'%d/%m/%y'),'-') AS activity_date,
COALESCE(SUM(CASE WHEN t.activity_name = 'Likes' THEN t.quantity END),'-') AS like_quantity,
COALESCE(SUM(CASE WHEN t.activity_name = 'Comments' THEN t.quantity END),'-') AS comment_activity
FROM your_master_table

LEFT JOIN 
(
    SELECT 
    customer_id,
    activity_date,
    No_of_likes AS quantity,
    'Likes' AS activity_name
    FROM TABLE_1

    UNION 

    SELECT 
    customer_id,
    activity_date,
    No_of_comments AS quantity,
    'Comments' AS activity_name
    FROM TABLE_2

) AS t
ON your_master_table.customer_id = t.customer_id
GROUP BY your_master_table.customer_id,t.activity_date
ORDER BY your_master_table.customer_id;

SQL FIDDLE DEMO