oracle 查询一个小组下的人都满足一个条件时返回一个值

供稿:hz-xin.com     日期:2024-05-18
oracle中如何只查询一条复合条件的记录,即查到一条记录就返回

可以用rownum来查询一条记录。
如emp表中有如下数据。

要求查询deptno为20的,但只取一条记录,可用如下语句:
select * from emp where deptno=20 and rownum<=1;查询结果:

用case和正则表达式可以处理,不知道列1的值为3时怎么处理,所以我没处理,你可以自己修改。
with t as( select 1 col1, 'a,d' col2 from dual union all select 1, null from dual union all select 2, 'b,c' from dual union all select 2, 'b,c' from dual union all select 2, null from dual union all select 3, 'a,c' from dual )select col1, case when col1 = 1 then case when col2 is null then 'a' when col2 is not null then regexp_substr(col2,'([a-b])') else col2 end when col1 = 2 then case when col2 is null then 'b' when col2 is not null then regexp_substr(col2,'([c-d])') else col2 end else col2 end from t;

select 字段名
from 表名
where not exists
(select 字段名
from 表名
where 字段 = 未完成任务的状态
)

最里面的括号查询的是 未完成状态的小组

然后嵌套进去 前面有个 not exists 代表不包括
就是查询不包括未完成状态的小组 那就是查询 完成状态的小组

找不在有不合格成绩的小组。
select * from 小组 z where where not exists (select 组号 from 组员成绩 c where 成绩=不合格 and c.组号=z.组号 group by 组号)