我有这个疑问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)
);
但都没有返回。任何帮助将非常感激。
请发布一些数据和您所需的输出将对我们有所帮助! – Buddi
我制作的版本@Tarun – DevKev
感谢您删除我的编辑内容,使您的问题更具可读性。 – APC