leetcode SQL练习记录:1045. 买下所有产品的客户

文章探讨了如何通过SQL查询找出在Customer表中购买了Product表中所有产品的客户ID。两种方法被提及,一种使用左连接和去重,另一种是通过对customer_id分组并比较产品键的计数。这两种方法都旨在找到购买了所有产品的独特客户ID。
摘要由CSDN通过智能技术生成

写一条 SQL 查询语句,从 Customer 表中查询购买了 Product 表中所有产品的客户的 id

思路:从 Customer 表中查询购买了 Product 表 ——> 两表联结,using(product_key)

          Product 表中所有产品 —— > 在c表中查询同一个id都购买了p表中商品

          顾客的id ——> distinct c.customer_id 

select distinct customer_id
from customer as c
left join product as p 
using(product_key)
where c.product_key = p.product_key

运行结果:会输入只买了6号产品的2id

 查看别人的代码如下:

select distinct customer_id
from customer 
group by customer_id
having count(distinct product_key) = (select count(*) as counts from product)

没有使用联结;对customer_id进行分组,之后过滤筛选出与去重的product_key合计数相等的所有数 —————俺看不懂

又找了一个代码,和上面那个差不多,但是我可以理解了。

select distinct customer_id
from customer 
group by customer_id
having count(distinct product_key) = (select count(product_key) from product)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值