我有一个包含category_id
列和is_required
列的表格,我希望选择category_id
的所有值,其中所有记录都有{ {1}}。
像is_required = 0
修改:另一种表达方法 - 我只需要选择从不在整个表格中SELECT category_id FROM table WHERE ALL is_required = 0
的{{1}}个值< / p>
答案 0 :(得分:1)
您可以使用not exists
:
select t.*
from table t
where not exists (select 1
from table t2
where t2.category_id = t.category_id and
(t2.is_required <> 0 or t2.is_required is null)
);
如果您只想要类别ID ,那么聚合可能会产生更合理的结果:
select category_id
from table t
group by category_id
having sum(t.is_required <> 0 or t.is_required is null) = 0;
答案 1 :(得分:0)
这就是你需要的吗?
select t1.category_id from `table` t1 left join `table` t2 on t1.category_id =t2.category_id and t1.is_required = 0 and t2.is_required = 1 where t2.category_id is null.