2015-10-14 147 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) 

看看这个。我想你没有加入到外面的桌子。