【问题标题】:SQL tricky querySQL 棘手查询
【发布时间】:2016-01-12 00:30:04
【问题描述】:

我有这个问题

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

但两者都没有返回任何内容。任何帮助将不胜感激。

【问题讨论】:

  • 请发布一些数据,您需要的输出将对我们有所帮助!!
  • 我制作的版本@Tarun
  • 感谢您删除我的编辑,这使您的问题更具可读性。
  • 对不起@APC,有人问我预期的输出,然后我看到我的版本被删除了,所以我回滚了。可能我应该让代码部分更具可读性

标签: sql oracle subquery not-exists


【解决方案1】:
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)

看看这个。我认为您没有加入外部表。

【讨论】:

    【解决方案2】:

    您的子查询返回行。你知道是因为第一个查询。但是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)
                   )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多