MySQL (SQL) |左联接不 RETURN 所有行(未应用过滤器)
MySQL (SQL) | LEFT JOIN DOES NOT RETURN ALL ROWS (NO FILTER APPLIED)
我想加入一个日历 table,销售额 table 和 return 用户每天的所有销售额(包括 没有销售额)。但是,出于某种原因,我只获得 销售 的天数。正如您在下面看到的,没有应用过滤器。我在这里做错了什么?
在我的 SQL 代码下方,所有 table 和希望的最终结果。
MySQL查询
SELECT
calendar.date AS date,
sales.user_id AS user_id,
CASE
WHEN sales.date = calendar.date THEN sales.quantity
ELSE 0
END AS quantity
FROM calendar
LEFT JOIN sales
ON calendar.date = sales.date
日期table(从 2020-01-01 到 2020-01-31)
date
2020-01-01
2020-01-02
...
2020-01-31
销售额table
date
user_id
quantity
2020-01-03
1
10
2020-01-12
1
12
2020-01-20
1
2
2020-01-01
2
13
2020-01-29
2
8
希望的结果
date
user_id
quantity
2020-01-01
1
0
2020-01-02
1
0
2020-01-03
1
10
2020-01-04
1
0
2020-01-05
1
0
2020-01-06
1
0
2020-01-07
1
0
2020-01-08
1
0
2020-01-09
1
0
2020-01-10
1
0
2020-01-11
1
0
2020-01-12
1
12
2020-01-13
1
0
2020-01-14
1
0
2020-01-15
1
0
2020-01-16
1
0
2020-01-17
1
0
2020-01-18
1
0
2020-01-19
1
0
2020-01-20
1
2
2020-01-21
1
0
2020-01-21
1
0
2020-01-22
1
0
2020-01-23
1
0
2020-01-24
1
0
2020-01-25
1
0
2020-01-26
1
0
2020-01-27
1
0
2020-01-28
1
0
2020-01-29
1
0
2020-01-30
1
0
2020-01-31
1
0
2020-01-01
2
13
2020-01-02
2
0
2020-01-03
2
0
2020-01-04
2
0
2020-01-05
2
0
2020-01-06
2
0
2020-01-07
2
0
2020-01-08
2
0
2020-01-09
2
0
2020-01-10
2
0
2020-01-11
2
0
2020-01-12
2
0
2020-01-13
2
0
2020-01-14
2
0
2020-01-15
2
0
2020-01-16
2
0
2020-01-17
2
0
2020-01-18
2
0
2020-01-19
2
0
2020-01-20
2
0
2020-01-21
2
0
2020-01-21
2
0
2020-01-22
2
0
2020-01-23
2
0
2020-01-24
2
0
2020-01-25
2
0
2020-01-26
2
0
2020-01-27
2
0
2020-01-28
2
0
2020-01-29
2
8
2020-01-30
2
0
2020-01-31
2
0
当天无销售用户也是未知数。要解决这个问题,您可以这样做:
SELECT
calendar.date AS date,
su.user_id AS user_id,
COALESCE( sales.quantity,0) AS quantity
FROM calendar
CROSS JOIN (SELECT DISTINCT user_id FROM sales) su
LEFT JOIN sales
ON calendar.date = sales.date AND sales.user_id=su.user_id
ORDER BY 2,1
我想加入一个日历 table,销售额 table 和 return 用户每天的所有销售额(包括 没有销售额)。但是,出于某种原因,我只获得 销售 的天数。正如您在下面看到的,没有应用过滤器。我在这里做错了什么?
在我的 SQL 代码下方,所有 table 和希望的最终结果。
MySQL查询
SELECT
calendar.date AS date,
sales.user_id AS user_id,
CASE
WHEN sales.date = calendar.date THEN sales.quantity
ELSE 0
END AS quantity
FROM calendar
LEFT JOIN sales
ON calendar.date = sales.date
日期table(从 2020-01-01 到 2020-01-31)
date |
---|
2020-01-01 |
2020-01-02 |
... |
2020-01-31 |
销售额table
date | user_id | quantity |
---|---|---|
2020-01-03 | 1 | 10 |
2020-01-12 | 1 | 12 |
2020-01-20 | 1 | 2 |
2020-01-01 | 2 | 13 |
2020-01-29 | 2 | 8 |
希望的结果
date | user_id | quantity |
---|---|---|
2020-01-01 | 1 | 0 |
2020-01-02 | 1 | 0 |
2020-01-03 | 1 | 10 |
2020-01-04 | 1 | 0 |
2020-01-05 | 1 | 0 |
2020-01-06 | 1 | 0 |
2020-01-07 | 1 | 0 |
2020-01-08 | 1 | 0 |
2020-01-09 | 1 | 0 |
2020-01-10 | 1 | 0 |
2020-01-11 | 1 | 0 |
2020-01-12 | 1 | 12 |
2020-01-13 | 1 | 0 |
2020-01-14 | 1 | 0 |
2020-01-15 | 1 | 0 |
2020-01-16 | 1 | 0 |
2020-01-17 | 1 | 0 |
2020-01-18 | 1 | 0 |
2020-01-19 | 1 | 0 |
2020-01-20 | 1 | 2 |
2020-01-21 | 1 | 0 |
2020-01-21 | 1 | 0 |
2020-01-22 | 1 | 0 |
2020-01-23 | 1 | 0 |
2020-01-24 | 1 | 0 |
2020-01-25 | 1 | 0 |
2020-01-26 | 1 | 0 |
2020-01-27 | 1 | 0 |
2020-01-28 | 1 | 0 |
2020-01-29 | 1 | 0 |
2020-01-30 | 1 | 0 |
2020-01-31 | 1 | 0 |
2020-01-01 | 2 | 13 |
2020-01-02 | 2 | 0 |
2020-01-03 | 2 | 0 |
2020-01-04 | 2 | 0 |
2020-01-05 | 2 | 0 |
2020-01-06 | 2 | 0 |
2020-01-07 | 2 | 0 |
2020-01-08 | 2 | 0 |
2020-01-09 | 2 | 0 |
2020-01-10 | 2 | 0 |
2020-01-11 | 2 | 0 |
2020-01-12 | 2 | 0 |
2020-01-13 | 2 | 0 |
2020-01-14 | 2 | 0 |
2020-01-15 | 2 | 0 |
2020-01-16 | 2 | 0 |
2020-01-17 | 2 | 0 |
2020-01-18 | 2 | 0 |
2020-01-19 | 2 | 0 |
2020-01-20 | 2 | 0 |
2020-01-21 | 2 | 0 |
2020-01-21 | 2 | 0 |
2020-01-22 | 2 | 0 |
2020-01-23 | 2 | 0 |
2020-01-24 | 2 | 0 |
2020-01-25 | 2 | 0 |
2020-01-26 | 2 | 0 |
2020-01-27 | 2 | 0 |
2020-01-28 | 2 | 0 |
2020-01-29 | 2 | 8 |
2020-01-30 | 2 | 0 |
2020-01-31 | 2 | 0 |
当天无销售用户也是未知数。要解决这个问题,您可以这样做:
SELECT
calendar.date AS date,
su.user_id AS user_id,
COALESCE( sales.quantity,0) AS quantity
FROM calendar
CROSS JOIN (SELECT DISTINCT user_id FROM sales) su
LEFT JOIN sales
ON calendar.date = sales.date AND sales.user_id=su.user_id
ORDER BY 2,1