0

我有这个查询,我相信可以对其进行优化:

SELECT floors.id, floors.floor FROM floors
WHERE floors.societies_id = 1
AND floors.status = 'Y'
AND floors.id NOT IN (
    SELECT DISTINCT(floors.id) FROM floors
    INNER JOIN societies ON societies.id = floors.societies_id
    INNER JOIN resident_floors ON resident_floors.floors_id = floors.id
    WHERE societies.id = 1
    AND floors.status = 'Y'
)

这个查询可以使用还是可以改进..?

4

1 回答 1

1

看起来您想要获取 resident_floors 中不存在的所有楼层。为此,我们可以将连接 RF 留在其中,并仅询问连接失败导致 RF 中为空的行:

SELECT floors.* FROM floors
INNER JOIN societies ON societies.id = floors.societies_id
LEFT JOIN resident_floors ON resident_floors.floors_id = floors.id
WHERE societies.id = 1
AND floors.status = 'Y'
AND resident_floors.floors_id IS NULL
于 2019-10-15T05:51:54.280 回答