简体   繁体   中英

discard SQL result based on the value of another table

I'm very lost when it comes to nested sql statements.

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.

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 ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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