LeetCode MySQL 1398. 购买了产品A和产品B却没有购买产品C的顾客

文章目录

    • 1. 题目
    • 2. 解题

1. 题目

Customers 表:

+---------------------+---------+
| Column Name         | Type    |
+---------------------+---------+
| customer_id         | int     |
| customer_name       | varchar |
+---------------------+---------+
customer_id 是这张表的主键。
customer_name 是顾客的名称。

Orders 表:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order_id      | int     |
| customer_id   | int     |
| product_name  | varchar |
+---------------+---------+
order_id 是这张表的主键。
customer_id 是购买了名为 "product_name" 产品顾客的id。

请你设计 SQL 查询来报告购买了产品 A 和产品 B 却没有购买产品 C 的顾客的 ID 和姓名( customer_id 和 customer_name ),我们将基于此结果为他们推荐产品 C 。

您返回的查询结果需要按照 customer_id 排序。

查询结果如下例所示。

Customers table:
+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 1           | Daniel        |
| 2           | Diana         |
| 3           | Elizabeth     |
| 4           | Jhon          |
+-------------+---------------+

Orders table:
+------------+--------------+---------------+
| order_id   | customer_id  | product_name  |
+------------+--------------+---------------+
| 10         |     1        |     A         |
| 20         |     1        |     B         |
| 30         |     1        |     D         |
| 40         |     1        |     C         |
| 50         |     2        |     A         |
| 60         |     3        |     A         |
| 70         |     3        |     B         |
| 80         |     3        |     D         |
| 90         |     4        |     C         |
+------------+--------------+---------------+

Result table:
+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 3           | Elizabeth     |
+-------------+---------------+
只有 customer_id 为 3 的顾客购买了产品 A 和产品 B ,却没有购买产品 C 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/customers-who-bought-products-a-and-b-but-not-c
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 注意 having 后面要用聚合函数做判断,不能having(product_name = 'A')
# Write your MySQL query statement below
select customer_id, customer_name
from Customers left join Orders
using(customer_id)
group by customer_id
having sum(product_name = 'A')>0
    and sum(product_name = 'B')>0
    and sum(product_name = 'C')=0
order by customer_id

or

# Write your MySQL query statement below
select customer_id, customer_name
from Customers 
where
    customer_id in (select customer_id from Orders where product_name='A')
    and
    customer_id in (select customer_id from Orders where product_name='B')
    and
    customer_id not in (select customer_id from Orders where product_name='C')
order by customer_id

我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

你可能感兴趣的:(LeetCode)