2015-10-14 150 views
0

我有這個疑問SQL查詢棘手

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中獲取沒有出現在此查詢中的所有記錄。比如我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) 
     ); 

但都沒有返回。任何幫助將非常感激。

+0

請發佈一些數據和您所需的輸出將對我們有所幫助! – Buddi

+0

我製作的版本@Tarun – DevKev

+0

感謝您刪除我的編輯內容,使您的問題更具可讀性。 – APC

回答

1

您的子查詢返回行。你知道這是因爲第一個查詢。但where 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) 
       ) 
+0

我得到了關於使用不存在的錯誤引用,謝謝APC! – DevKev

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) 

看看這個。我想你沒有加入到外面的桌子。