SQL 查找房间未满的具体日期

SQL Find specific date where rooms are not fully taken

我有两个 table:

一个 table 是这样的: Table 名称保留:

Name Room Seats Date
John 101  20    11/22

Table 命名房间:

Name Seats
101  30
202  40

SELECT *
FROM Reservations
WHERE date = '2020-11-22' AND
SELECT Reservations.Room
FROM Reservations
WHERE Reservations.Room NOT IN(SELECT Room.Name FROM Room)

我试过上面的语句,但是没有用。我希望命令 return 日期中所有未使用的房间。应该是 return 202 房间。就这样。

我了解到您想要在指定日期还有剩余座位的房间。一种选择是使用子查询进行过滤:

select ro.*
from rooms ro
where ro.seats > (
    select coalesce(sum(re.seats), 0)
    from reservations re
    where re.room = ro.name and re.date = '2020-11-12'
)

另一方面,如果您想要在给定日期没有任何预订的房间,请使用 not exists:

select ro.*
from rooms ro
where not exists (
    select 1
    from reservations re
    where re.room = ro.name and re.date = '2020-11-12'
)