簡體   English   中英

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)

返回:

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

它正是我想要的。 現在,我想從此查詢中未顯示的表labatorios中獲取所有記錄。 例如,我的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)
       );

但兩者均不返回任何內容。 任何幫助將不勝感激。

您的子查詢返回行。 您知道這是因為有第一個查詢。 但是where not exists只有當子查詢不返回rowwhere not exists的地方才成立。 看看這個:

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

看一下這個。 我認為您沒有加入外部表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM