SQL棘手的查询

时间:2015-10-14 04:14:30

标签: sql oracle subquery not-exists

我有这个查询

select lab.IDLAB,
       lab.NOMLAB,
       lab.CAPACIDAD
  from laboratorio lab 
 inner join DETALLESOLICITUD det on det.IDLAB = lab.IDLAB
 inner join dia on dia.iddia = det.iddia 
 inner join bloque blo on blo.idbloque = det.idbloque 
 inner join solicitud sol on sol.IDSOLICITUD=det.IDSOLICITUD
 where blo.idbloque = 1 
   and dia.iddia = 1
   and sol.estado in (1,2)

返回:

 IDLAB | NOMLAB | CAPACIDAD
----------------------------
 1     | LCOMP1 | 22

它完全符合我的要求。现在我想从表查询器中获取未出现在此查询中的所有记录。例如,我的laboratorios表包含内容:

 IDLAB | NOMLAB | CAPACIDAD
----------------------------
 1     | LCOMP1 | 22
 2     | LCOMP2 | 31
 3     | LCOMP3 | 17
 4     | LCOMP4 | 26

我想要以下输出:

 IDLAB | NOMLAB | CAPACIDAD
----------------------------
 2     | LCOMP2 | 31
 3     | LCOMP3 | 17
 4     | LCOMP4 | 26

我尝试用这样的not exists语句:

select *
  from laboratorio
 where not exists(
        select lab.idlab, lab.nomlab, lab.CAPACIDAD 
          from laboratorio lab
         inner join DETALLESOLICITUD det on det.idlab = lab.idlab
         inner join dia on dia.iddia = det.iddia 
         inner join bloque blo on blo.idbloque = det.idbloque
         inner join solicitud sol on sol.IDSOLICITUD = det.IDSOLICITUD
         where blo.idbloque = 1 
           and dia.iddia = 1
           and sol.estado in(1,2)
       );

这样:

select *
  from laboratorio
 where not exists(
        select det.IDLAB
          from DETALLESOLICITUD det
         inner join dia on dia.iddia = det.iddia
         inner join bloque blo on blo.idbloque = det.idbloque 
         inner join solicitud sol on sol.IDSOLICITUD = det.IDSOLICITUD
         where blo.idbloque = 1
           and dia.iddia = 1
           and sol.estado in(1,2)
       );

但两者都没有返回。任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:1)

您的子查询返回行。你知道,因为第一个查询。但是where not exists仅在子查询返回 no rows 时才为真。看看:

SQL> select * from dual
  2  /

D
-
X

SQL>  select * from dual
  2  where not exists (select * from dual
  3                    where dummy = 'X')
  4  /

no rows selected

SQL> select * from dual
  2  where not exists (select * from dual
  3                    where dummy = 'Y')
  4  /

D
-
X

SQL> 

所以你需要做的是将外部查询与子查询相关联。最简单的方法:

select * from laboratorio 
where (idlab, nomlab, CAPACIDAD)
       not in (select lab.idlab, lab.nomlab, lab.CAPACIDAD 
                from laboratorio lab 
                inner join DETALLESOLICITUD det on  det.idlab = lab.idlab
                inner join dia on dia.iddia = det.iddia 
                inner join bloque blo on blo.idbloque = det.idbloque 
                inner join solicitud sol on sol.IDSOLICITUD=det.IDSOLICITUD                    
                where blo.idbloque = 1 
                and dia.iddia = 1 
                and sol.estado in(1,2)
               )

答案 1 :(得分:0)

select * from laboratorio lab1 WHERE NOT EXISTS (
select 1 from laboratorio lab 
inner join DETALLESOLICITUD det on det.IDLAB = lab.IDLAB
inner join dia on dia.iddia = det.iddia inner join bloque blo on blo.idbloque = det.idbloque 
inner join solicitud sol on sol.IDSOLICITUD=det.IDSOLICITUD 
where blo.idbloque = 1 and dia.iddia = 1 and sol.estado in (1,2)
  AND lab1.rowid = lab.rowid)

检查一下。我想你没有加入外围桌。