当前位置: 代码迷 >> 综合 >> 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_idand  exists (SELECT * FROM orders WHERE product_name = 'A' and customer_id = o.customer_id)and  exists (SELECT * FROM orders WHERE product_name = 'B' and customer_id = o.customer_id)and  not exists(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') > 0 and SUM(product_name = 'B') > 0 and SUM(product_name = 'C') = 0
  相关解决方案