【Oracle】查询表总某列是否全部满足条件;

1、问题

求某部门下的员工是否都交了保险;

2、解答

查询部门总人数

select count(1)  from dept a,emp b
where a.deptno = b.deptno
and  a.deptno = 01

查询交了保险了部门总人数

select count(1) from dept a ,emp b
where a.deptno = b.deptno
and a.deptno = 01
and b.isinsurance = 'Y';

求两者的差,如果为0,则表示全部交了保险,返回true;否则就是没交,返回false;

select decode (
(select count(1) from dept  a ,emp b
where a.deptno = b.deptno
and a.deptno = 01) 
-
(select count(1) from dept  a ,emp b
where a.deptno = b.deptno
and a.deptno = 01
and b.isinsurance ='Y')
,0,'TRUE','FALSE'
) from dual;

3、分析

这里的

decode(计算公式,正确结果,返回正确结果的信息,返回错误结果的信息);

猜你喜欢

转载自blog.csdn.net/River_Continent/article/details/81395106