简体   繁体   English

根据另一个表的值丢弃 SQL 结果

[英]discard SQL result based on the value of another table

I'm very lost when it comes to nested sql statements.当涉及到嵌套的 sql 语句时,我非常迷茫。

I'm currently building a database to manage executive suites and I'd like to be able to find vacant suites by comparing what I have leased vs the total suites I have available.我目前正在建立一个数据库来管理行政套房,我希望能够通过比较我租用的套房与可用套房的总数来找到空置套房。

This is the best I've been able to come up with:这是我能想到的最好的:

SELECT * FROM `executiveSuites` 
LEFT JOIN `executiveLease` 
ON executiveLease.suiteNumber = executiveSuites.SuiteNumber 
AND executiveLease.Property = executiveSuites.BuildingID 
WHERE NOT EXISTS 
( SELECT * FROM `executiveLease` WHERE executiveLease.status = 1 )  

the expected result would be that any suite that doesn't have a lease attached with a status of 1 would show up but what I'm getting is a blank list.预期的结果是,任何没有附加状态为 1 的租约的套房都会出现,但我得到的是一个空白列表。

I'm self taught and don't know what I don't know, so any help or at the least a point in the right direction is very much appreciated.我是自学的,不知道我不知道什么,因此非常感谢任何帮助或至少在正确方向上的一点。

Looks like you need in看起来你需要在

SELECT * 
FROM `executiveSuites` es
WHERE NOT EXISTS ( SELECT NULL
                   FROM `executiveLease` el
                   WHERE el.suiteNumber = es.SuiteNumber 
                     AND el.Property = es.BuildingID 
                     AND el.status = 1 ) 

or要么

SELECT * 
FROM `executiveSuites` es
LEFT JOIN `executiveLease` el ON el.suiteNumber = es.SuiteNumber 
                             AND el.Property = es.BuildingID
WHERE NOT EXISTS ( SELECT NULL 
                   FROM `executiveLease` el_s
                   WHERE el_s.suiteNumber = es.SuiteNumber 
                     AND el_s.Property = es.BuildingID 
                     AND el_s.status = 1 ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM