0

我有这个选择语句:

SELECT *
FROM Room
LEFT JOIN booking ON (Room.RoomId = booking.RoomId)
WHERE booking.Roomid is null
  AND GETDATE() BETWEEN bookin.checkindate '" + TxtCheckIn.Text + "'
                AND booking.checkoutdate  '" + TxtCheckOut.Text + "'" + "
ORDER BY Room.RoomType

如果日期与用户选择的入住和退房日期匹配,我想检查预订表。如果不匹配,则查询应显示房间表中的所有房间(即使它在预订表中),前提是它们具有不同的日期。

4

1 回答 1

0

您需要确定是否有任何行与日期的条件匹配。为此,以下查询将日期条件移动到on子句中。然后它使用窗口函数count()来计算匹配的数量。如果没有,则返回所有行。否则,仅返回匹配项。

select t.*
from (SELECT *, count(booking.RoomId) over () as numMatches
      FROM Room LEFT JOIN
           booking
           ON Room.RoomId = booking.RoomId and
              GETDATE() BETWEEN booking.checkindate '" + TxtCheckIn.Text + "' and
                                booking.checkoutdate  '" + TxtCheckOut.Text + "'" + "
     ) t
where numMatches > 0 and RoomId is not null or numMatches = 0
ORDER BY Room.RoomType
于 2013-07-25T11:17:19.163 回答