当前位置: 代码迷 >> 综合 >> Leetcode Mysql 1398. 购买了产品 A 和产品 B 却没有购买产品 C 的顾客(DAY 7)
详细解决方案
Leetcode Mysql 1398. 购买了产品 A 和产品 B 却没有购买产品 C 的顾客(DAY 7)
热度:22 发布时间:2023-11-17 17:06:39.0
文章目录
原题题目
代码实现(首刷自解)
代码实现(首刷优化)
原题题目
代码实现(首刷自解)
#Write your MySQL query statement below
SELECT
DISTINCT c.customer_id,c.customer_name
FROMcustomers c
JOIN orders o
WHEREc.customer_id = o.customer_idandexists(SELECT * FROM orders WHERE product_name ='A'and customer_id = o.customer_id)andexists(SELECT * FROM orders WHERE product_name ='B'and customer_id = o.customer_id)andnotexists(SELECT * FROM orders WHERE product_name ='C'and customer_id = o.customer_id)
代码实现(首刷优化)
#Write your MySQL query statement below
SELECT
DISTINCT c.customer_id,c.customer_name
FROMcustomers c JOIN orders o on c.customer_id = o.customer_id
GROUP BYo.customer_id
HAVINGSUM(product_name ='A')>0andSUM(product_name ='B')>0andSUM(product_name ='C')=0