1 Star 0 Fork 0

逸扬/leetcode-hub-mysql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc-1398.test 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
逸扬 提交于 3年前 . 619-1112-1280-1398-1607 (5)
# $1398. 购买了产品 A 和产品 B 却没有购买产品 C 的顾客
# https://leetcode-cn.com/problems/customers-who-bought-products-a-and-b-but-not-c/
# SQL架构
Create table If Not Exists Customers (customer_id int, customer_name varchar(30));
Create table If Not Exists Orders (order_id int, customer_id int, product_name varchar(30));
Truncate table Customers;
insert into Customers (customer_id, customer_name) values ('1', 'Daniel');
insert into Customers (customer_id, customer_name) values ('2', 'Diana');
insert into Customers (customer_id, customer_name) values ('3', 'Elizabeth');
insert into Customers (customer_id, customer_name) values ('4', 'Jhon');
Truncate table Orders;
insert into Orders (order_id, customer_id, product_name) values ('10', '1', 'A');
insert into Orders (order_id, customer_id, product_name) values ('20', '1', 'B');
insert into Orders (order_id, customer_id, product_name) values ('30', '1', 'D');
insert into Orders (order_id, customer_id, product_name) values ('40', '1', 'C');
insert into Orders (order_id, customer_id, product_name) values ('50', '2', 'A');
insert into Orders (order_id, customer_id, product_name) values ('60', '3', 'A');
insert into Orders (order_id, customer_id, product_name) values ('70', '3', 'B');
insert into Orders (order_id, customer_id, product_name) values ('80', '3', 'D');
insert into Orders (order_id, customer_id, product_name) values ('90', '4', 'C');
# Write your MySQL query statement below
select
customer_id,
customer_name
from
Customers
where
customer_id in (
select
customer_id
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;
# clean-up
drop table Customers;
drop table Orders;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gdut_yy/leetcode-hub-mysql.git
git@gitee.com:gdut_yy/leetcode-hub-mysql.git
gdut_yy
leetcode-hub-mysql
leetcode-hub-mysql
master

搜索帮助