简体   繁体   English

Oracle 查询获取不在结果集中的记录

[英]Oracle Query to get the records which are not in the results sets

I have a scenario where I need to query the table to see the how many records are not present from the particular record set.我有一个场景,我需要查询表以查看特定记录集中不存在多少记录。

for eg -I have a record set (deptno1,deptno2....deptno100)例如 - 我有一个记录集 (deptno1,deptno2....deptno100)

Table - Emp1表 - Emp1

I would like to know how many records from that record set are not present in the table emp1.我想知道该记录集中有多少记录没有出现在表 emp1 中。

If I run this query - select * from emp1 where deptno notin(deptno1....deptno100) it gives the records other than 100 records eventhough those 100 records might be in that table如果我运行这个查询 - select * from emp1 where deptno notin(deptno1....deptno100) 它给出的记录不是 100 条记录,尽管这 100 条记录可能在该表中

You can use:您可以使用:

WITH data (deptno) AS (
  SELECT 'deptno1' FROM DUAL UNION ALL
  SELECT 'deptno2' FROM DUAL UNION ALL
  -- ...
  SELECT 'deptno100' FROM DUAL
)
SELECT d.deptno
FROM   data d
       LEFT OUTER JOIN emp1 e
       ON (d.deptno = e.deptno)
WHERE  e.deptno IS NULL;

You can generate the values using connect by.您可以使用 connect by 生成值。

with d(dept) AS ( select 'deptno' || to_char(level) from dual connect by level <= 100 ) select d.dept from d left join emp1 e on (d.dept = e.deptno) where e.deptno is null;使用 d(dept) AS (select 'deptno' || to_char(level) from dual connect by level <= 100) select d.dept from d left join emp1 e on (d.dept = e.deptno)是 null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM