How to check if value exists in each group (after group by)

后端 未结 3 1975
慢半拍i
慢半拍i 2020-12-28 08:39

Assume I have a subscriptions table :

uid  | subscription_type 
------------------------  
Alex | type1
Alex | type2
Alex | type3
Alex | type4
Ben  | type2
B         


        
3条回答
  •  孤城傲影
    2020-12-28 09:15

    To check if something doesn't exist, use NOT EXISTS(...):

    SELECT uid
    FROM subscribes su
    WHERE NOT EXISTS (SELECT *
            FROM subscribes nx
            WHERE nx.uid = su.uid AND nx.subscription_type = 'type1'
            )
    GROUP BY uid HAVING COUNT(*) > 2
            ;
    

提交回复
热议问题