如何通过拥有相同的ID进行分组?

sim*_*ico 1 sql postgresql

我想要从以下架构购买产品X和Y和Z的customerid:

销售(customerid,productName,rid);

我可以做交叉点:

select customerid from sales where productName='X' 
INTERSECT 
select customerid from sales where productName='X' 
INTERSTECT
select customerid from sales where productName='Z'
Run Code Online (Sandbox Code Playgroud)

这是我能做的最好的吗?

Red*_*ter 6

不确定这是否适用于postrgesql,但请尝试:

select customerid 
from sales 
where productName in ('X', 'Y', 'Z')
group by customerid
having count(distinct productName) = 3
Run Code Online (Sandbox Code Playgroud)