来自mysql中特定组的结果

wz1wpwve  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(271)
Suppose I have following information,

# PAN#       #Product#

AAAAAAAAAA  Product1
AAAAAAAAAA  Product2
AAAAAAAAAA  Product3
AAAAAAAAAA**Product4**
AAAAAAAAAA  Product5
BBBBBBBBBB  Product1
BBBBBBBBBB  Product2
BBBBBBBBBB  Product3
BBBBBBBBBB**Product4**
BBBBBBBBBB  Product5
CCCCCCCCCC  Product1
DDDDDDDDDD  Product2

我只想要那些产品<>'product4'(ccccccccccccccccccccccccccccccccccccccccccccccdddddddddddd)的锅,但它给我的结果是错误的,因为同样的锅也在其他产品(product1,product2,product3,product5)中。所以不是给出结果(ccccccccccccccccccccdddddddddd),而是给出(aaaaaaaaaaaaaabbbbbbbbccccccccccccdddddddddd)

ljo96ir5

ljo96ir51#

左连接的另一种方法

select a.*
from table1 a
left join table1 b on a.PAN = b.PAN and b.Product = 'Product4'
where b.PAN is null

或者使用条件聚合

select a.PAN
from table1 a
group by PAN
having count(case when Product = 'Product4' then 1 else null end) = 0

演示

bvuwiixz

bvuwiixz2#

我相信你需要这样的东西:

SELECT PAN from myTable 
WHERE PAN NOT IN ( SELCT PAN from myTable 
WHERE Product ='Product4')

相关问题