具有 JOIN 的 SQLite SELECT 仅当第二 table 中的每一行对于 WHERE 子句都为真时才需要 return 行

SQLite SELECT with JOIN needs to return row only if every row in second table is true for WHERE clause

即使第二行只有一行table不符合日期条件,我也需要select为空。但是每次停车space 在 Reservations table 中有多个预订,即使其中一个预订符合不在范围内的条件,我也会获得停车 ID space。

我的SELECT

SELECT Parking_Spaces.ID_Parking_Space FROM Reservations 

INNER JOIN Parking_Spaces ON Reservations.ID_Parking_space = Parking_Spaces.ID_Parking_Space
AND (Reservations.Arrival_time NOT BETWEEN "2000-12-15" AND "2005-12-15")
AND (Reservations.Departure_time NOT BETWEEN "2000-12-15" AND "2005-12-15")

Table 示例:我不想从这个 table SELECT ID_Parking_Space,因为第一行不符合我的条件。但是因为第二个我确实得到了ID。

|ID_RESERVATION |  Arrival_time  |    Departure_time   |  ID_Parking_Space
|:------------- |:--------------:|: ------------------:|:----------------:|
|      6        |   2001-12-15   |    2002-12-15       |        4         |
|      16       |   2009-12-15   |    2010-12-15       |        4         |

在完整的 table 中有更多的保留,如果有一行不满足条件,我需要 select 失败。

我想你需要的是NOT EXISTS:

SELECT p.ID_Parking_Space 
FROM Parking_Spaces p 
WHERE NOT EXISTS (
  SELECT 1
  FROM Reservations r 
  WHERE r.ID_Parking_space = p.ID_Parking_Space
  AND (
    r.Arrival_time BETWEEN '2000-12-15' AND '2005-12-15' 
    OR 
    r.Departure_time BETWEEN '2000-12-15' AND '2005-12-15'
  )
);