-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1083.sql
More file actions
42 lines (40 loc) · 949 Bytes
/
LeetCode1083.sql
File metadata and controls
42 lines (40 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Write your MySQL query statement below
-- 按照buyer_id求差集
select t1.buyer_id
from (
select DISTINCT
s.buyer_id
from Sales s
left join Product p on s.product_id = p.product_id
where
p.product_name = "S8"
) t1
left join (
select DISTINCT
s.buyer_id
from Sales s
left join Product p on s.product_id = p.product_id
where
p.product_name = "iPhone"
) t2 on t1.buyer_id = t2.buyer_id
where
t2.buyer_id is null;
--直接按buyer_id分组后按照次数筛选
SELECT s.buyer_id
FROM Sales AS s
INNER JOIN Product AS p ON s.product_id = p.product_id
GROUP BY
s.buyer_id
HAVING
SUM(
CASE
WHEN p.product_name = 'S8' THEN 1
ELSE 0
END
) > 0
AND SUM(
CASE
WHEN p.product_name = 'iPhone' THEN 1
ELSE 0
END
) = 0;