Leetcode力扣 MySQL数据库 1398 购买产品A和产品B却没有购买产品C的顾客

1398 购买产品A和产品B却没有购买产品C的顾客


SQL架构

Create table If Not Exists Customers_1398 (customer_id int, customer_name varchar(30));
Create table If Not Exists Orders_1398 (order_id int, customer_id int, product_name varchar(30));
Truncate table Customers_1398;
insert into Customers_1398 (customer_id, customer_name) values ('1', 'Daniel');
insert into Customers_1398 (customer_id, customer_name) values ('2', 'Diana');
insert into Customers_1398 (customer_id, customer_name) values ('3', 'Elizabeth');
insert into Customers_1398 (customer_id, customer_name) values ('4', 'Jhon');
Truncate table Orders_1398;
insert into Orders_1398 (order_id, customer_id, product_name) values ('10', '1', 'A');
insert into Orders_1398 (order_id, customer_id, product_name) values ('20', '1', 'B');
insert into Orders_1398 (order_id, customer_id, product_name) values ('30', '1', 'D');
insert into Orders_1398 (order_id, customer_id, product_name) values ('40', '1', 'C');
insert into Orders_1398 (order_id, customer_id, product_name) values ('50', '2', 'A');
insert into Orders_1398 (order_id, customer_id, product_name) values ('60', '3', 'A');
insert into Orders_1398 (order_id, customer_id, product_name) values ('70', '3', 'B');
insert into Orders_1398 (order_id, customer_id, product_name) values ('80', '3', 'D');
insert into Orders_1398 (order_id, customer_id, product_name) values ('90', '4', 'C');


 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 。


解题

SELECT
    c.customer_id,c.customer_name
FROM
    Orders_1398 o LEFT JOIN Customers_1398 c ON o.customer_id = c.customer_id
GROUP BY c.customer_id,c.customer_name
HAVING
    SUM(product_name = 'A') * SUM(product_name = 'B') > 0
    AND SUM(product_name = 'C') = 0
ORDER BY c.customer_id;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ziko-1101

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值