假设我有一个订阅表:
uid | subscription_type ------------------------ Alex | type1 Alex | type2 Alex | type3 Alex | type4 Ben | type2 Ben | type3 Ben | type4
并且只想选择订阅数量超过2个但从未订阅过类型1的用户
预期结果是仅选择“ Ben”。
我很容易找到使用2个以上订阅的用户:
SELECT uid FROM subscribes GROUP BY uid HAVING COUNT(*) > 2
但是,如何检查组中是否不存在某些值?
谢谢您的帮助!
试试这个查询:
SELECT uid FROM subscribes GROUP BY uid HAVING COUNT(*) > 2 AND max( CASE "subscription_type" WHEN 'type1' THEN 1 ELSE 0 END ) = 0