查询
select brand
from products
where status = false
and brand not in (
select brand
from products
where status = true
group by brand
)
group by brand;
基本上,我想只选择那些在任何产品中都没有status = true的品牌。
我想优化上述查询
答案 0 :(得分:2)
使用布尔聚合函数bool_or()
:
select brand
from products
group by brand
having not bool_or(status);
答案 1 :(得分:1)
您可以使用CASE
语句将布尔值转换为位,然后使用MAX
:
SELECT brand
FROM products
GROUP BY brand
HAVING MAX(CASE WHEN status = TRUE THEN 1 ELSE 0 END) = 1