力扣高频SQL 50题(基础版)第三十题

力扣高频SQL 50题(基础版)第三十题

1045.买下所有产品的客户

题目说明

Customer 表:

±------------±--------+

| Column Name | Type |

±------------±--------+

| customer_id | int |

| product_key | int |

±------------±--------+

该表可能包含重复的行。

customer_id 不为 NULL。

product_key 是 Product 表的外键(reference 列)。

Product 表:

±------------±--------+

| Column Name | Type |

±------------±--------+

| product_key | int |

±------------±--------+

product_key 是这张表的主键(具有唯一值的列)。

编写解决方案,报告 Customer 表中购买了 Product 表中所有产品的客户的 id。

返回结果表 无顺序要求

实现过程

准备数据
Create table If Not Exists Customer (customer_id int, product_key int)
Create table Product (product_key int)
Truncate table Customer
insert into Customer (customer_id, product_key) values ('1', '5')
insert into Customer (customer_id, product_key) values ('2', '6')
insert into Customer (customer_id, product_key) values ('3', '5')
insert into Customer (customer_id, product_key) values ('3', '6')
insert into Customer (customer_id, product_key) values ('1', '6')
Truncate table Product
insert into Product (product_key) values ('5')
insert into Product (product_key) values ('6')
实现方式
select c.customer_id
from Customer c join Product p
    on c.product_key=p.product_key
    group by c.customer_id
    having count(distinct c.product_key)=(select count(distinct product_key) from Product);
结果截图

在这里插入图片描述

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值