sql判断记录是否存在

1083. 销售分析 II - 力扣(LeetCode)

写出几种解法以供参考:主要是0,1的灵活运用,存在为1,不存在为0 的思维;

这里的第一种应该是第二种的简写。

select s.buyer_id 
from sales as s left join product as p 
on s.product_id=p.product_id
group s.by buyer_id
having sum(p.product_name='S8')>0 and sum(p.product_name='iPhone')=0
select s.buyer_id 
from Sales s left join Product p 
on s.product_id = p.product_id 
group by s.buyer_id 
having sum(if(product_name = 'S8',1,0)) > 0 and sum(if(product_name = 'iPhone',1,0))=0
select s.buyer_id 
from sales as s left join product as p 
on s.product_id=p.product_id
group by s.buyer_id
having sum(case when p.product_name='S8'  then 1 else 0 end)>0
and sum(case when p.product_name='iPhone' then 1 else 0 end)=0;

 

 

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页