SQL 棘手的查询

SQL tricky query

我有这个问题

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)

哪个returns:

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

而且它完全符合我的要求。现在我想从 table laboratorios 中获取所有没有出现在这个查询中的记录。例如我的 laboratorios table 有内容:

 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)
       );

但两者都returns什么都没有。任何帮助将不胜感激。

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)

看看这个。我想你没有加入外table.

您的子查询 return 行。你知道这是因为第一个查询。但是 where not exists 仅当子查询 returns 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)
               )