SQL按辅助ID类型进行过滤

时间:2018-12-18 09:35:22

标签: sql oracle

我有这张桌子:

id idsec type
1  a     color
2  a     description
3  b     description
4  b     food
5  b     animal
6  c     color
7  d     descritpion

我想选择所有没有按idsec分组的“颜色”类型的idsec。

4 个答案:

答案 0 :(得分:2)

您可以使用select t.* from table t where not exists (select 1 from table t1 where t1.idsec = t.idsec and t1.type = 'color');

select idsec
from table t
group by idsec
having sum(case when type = 'color' then 1 else 0 end) = 0;

您也可以进行聚合:

Graphene.guardAjax(buttonToClick).click();

答案 1 :(得分:1)

使用以下代码:-

select idsec from table 
where idsec not in (select idsec from table where type = 'color')
group by idsec

答案 2 :(得分:0)

一个选择是使用minus集合运算符

with t(id,idse,type) as
(
 select 1,'a','color' from dual union all
 select 2,'a','description' from dual union all
 select 3,'b','description' from dual union all
 select 4,'b','food' from dual union all
 select 5,'b','animal' from dual union all
 select 6,'c','color' from dual union all
 select 7,'d','descritpion' from dual
)
select idse from t group by idse
minus   
select idse from t where type = 'color';

IDSE
----
 b
 d

select distinct idse from t
minus   
select idse from t where type = 'color';

IDSE
----
 b
 d

答案 3 :(得分:0)

您需要表中idsec的不同值:

SELECT 
  DISTINCT idsec 
FROM tablename t 
WHERE
  NOT EXISTS (
    SELECT 1 FROM tablename WHERE tablename.idsec = t.idsec AND tablename.type = 'color'
  );