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..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 @@ -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; ``` 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; ``` 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;

AltStyle によって変換されたページ (->オリジナル) /