Skip to content

Commit 52193fa

Browse files
authored
feat: update sql solution to lc problem: No.1398 (#1780)
1 parent bfd80c4 commit 52193fa

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,23 @@ Orders table:
8585

8686
<!-- 这里可写通用的实现逻辑 -->
8787

88+
**方法一:LEFT JOIN + GROUP BY + HAVING**
89+
90+
我们可以用 `LEFT JOIN``Customers` 表和 `Orders` 表连接起来,然后按照 `customer_id` 进行分组,最后筛选出购买了产品 A 和产品 B 却没有购买产品 C 的顾客。
91+
8892
<!-- tabs:start -->
8993

9094
### **SQL**
9195

9296
```sql
9397
# Write your MySQL query statement below
94-
SELECT
95-
customer_id,
96-
customer_name
98+
SELECT customer_id, customer_name
9799
FROM
98-
Orders
99-
JOIN Customers USING (customer_id)
100+
Customers
101+
LEFT JOIN Orders USING (customer_id)
100102
GROUP BY 1
101-
HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0;
103+
HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0
104+
ORDER BY 1;
102105
```
103106

104107
<!-- tabs:end -->

solution/1300-1399/1398.Customers Who Bought Products A and B but Not C/README_EN.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,23 @@ Orders table:
7979

8080
## Solutions
8181

82+
**Solution 1: LEFT JOIN + GROUP BY + HAVING**
83+
84+
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.
85+
8286
<!-- tabs:start -->
8387

8488
### **SQL**
8589

8690
```sql
8791
# Write your MySQL query statement below
88-
SELECT
89-
customer_id,
90-
customer_name
92+
SELECT customer_id, customer_name
9193
FROM
92-
Orders
93-
JOIN Customers USING (customer_id)
94+
Customers
95+
LEFT JOIN Orders USING (customer_id)
9496
GROUP BY 1
95-
HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0;
97+
HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0
98+
ORDER BY 1;
9699
```
97100

98101
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
customer_id,
4-
customer_name
2+
SELECT customer_id, customer_name
53
FROM
6-
Orders
7-
JOIN Customers USING (customer_id)
4+
Customers
5+
LEFT JOIN Orders USING (customer_id)
86
GROUP BY 1
9-
HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0;
7+
HAVING sum(product_name = 'A') > 0 AND sum(product_name = 'B') > 0 AND sum(product_name = 'C') = 0
8+
ORDER BY 1;

0 commit comments

Comments
 (0)