From 896ccbccdbabca37479a5b2dfb066cf526994791 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 10 Oct 2023 17:05:57 +0800 Subject: [PATCH 1/4] feat: update sql solution to lc problem: No.1398 --- .../README.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README.md b/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README.md index bb9963e8e3945..8dccfac210940 100644 --- a/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README.md +++ b/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README.md @@ -85,20 +85,23 @@ Orders table: +**方法一:LEFT JOIN + GROUP BY + HAVING** + +我们可以用 `LEFT JOIN` 将 `Customers` 表和 `Orders` 表连接起来,然后按照 `customer_id` 进行分组,最后筛选出购买了产品 A 和产品 B 却没有购买产品C 的顾客。 + ### **SQL** ```sql # Write your MySQL query statement below -SELECT - customer_id, - customer_name +SELECT customer_id, customer_name FROM - Orders - JOIN Customers USING (customer_id) + Customers + LEFT JOIN Orders USING (customer_id) GROUP BY 1 -HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0; +HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0 +ORDER BY 1; ``` From 49584a09cbd3086885afd9e0a747cd40cf961c58 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 10 Oct 2023 17:07:13 +0800 Subject: [PATCH 2/4] Update README_EN.md --- .../README_EN.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README_EN.md b/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README_EN.md index 67d998fa9d569..3ec0ebb5d65fa 100644 --- a/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README_EN.md +++ b/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README_EN.md @@ -79,20 +79,23 @@ Orders table: ## Solutions +**Solution 1: LEFT JOIN + GROUP BY + HAVING** + +We can use `LEFT JOIN` to join the `Customers` table and the `Orders` table, then group them by `customer_id`, and finally filter out the customers who have purchased products A and B but not product C. + ### **SQL** ```sql # Write your MySQL query statement below -SELECT - customer_id, - customer_name +SELECT customer_id, customer_name FROM - Orders - JOIN Customers USING (customer_id) + Customers + LEFT JOIN Orders USING (customer_id) GROUP BY 1 -HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0; +HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0 +ORDER BY 1; ``` From 5a33506ed53dc5805f47a6f0ed413740a441b839 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 10 Oct 2023 17:07:56 +0800 Subject: [PATCH 3/4] Update Solution.sql --- .../Solution.sql | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/Solution.sql b/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/Solution.sql index 07a4aca868cd6..735f6646cd394 100644 --- a/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/Solution.sql +++ b/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/Solution.sql @@ -1,9 +1,8 @@ # Write your MySQL query statement below -SELECT - customer_id, - customer_name +SELECT customer_id, customer_name FROM - Orders - JOIN Customers USING (customer_id) + Customers + LEFT JOIN Orders USING (customer_id) GROUP BY 1 -HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0; +HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0 +ORDER BY 1; From 9f2c3c700c7c27d837951de688e3851c6870a36a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 10 Oct 2023 09:11:16 +0000 Subject: [PATCH 4/4] style: format code and docs with prettier --- .../README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README.md b/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README.md index 8dccfac210940..521ea163e5a85 100644 --- a/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README.md +++ b/solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README.md @@ -87,7 +87,7 @@ Orders table: **方法一:LEFT JOIN + GROUP BY + HAVING** -我们可以用 `LEFT JOIN` 将 `Customers` 表和 `Orders` 表连接起来,然后按照 `customer_id` 进行分组,最后筛选出购买了产品 A 和产品 B 却没有购买产品C 的顾客。 +我们可以用 `LEFT JOIN` 将 `Customers` 表和 `Orders` 表连接起来,然后按照 `customer_id` 进行分组,最后筛选出购买了产品 A 和产品 B 却没有购买产品 C 的顾客。