查找具有值1和2但不是3的同一个表中的值

时间:2016-04-04 01:35:33

标签: sql

嗨我有下面的表格,我想找到我有format_id 1,3,11和12但不是10的object_ida。你能帮忙吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用group byhaving

select object_id
from t
group by object_id
having sum(case when format_id = 1 then 1 else 0 end) > 0 and
       sum(case when format_id = 3 then 1 else 0 end) > 0 and
       sum(case when format_id = 11 then 1 else 0 end) > 0 and
       sum(case when format_id = 12 then 1 else 0 end) > 0 and
       sum(case when format_id = 10 then 1 else 0 end) = 0;

每个条件都会测试其中一个format_id> 0表示至少有一个分配给object_id= 0表示没有分配任何内容。