2013-07-25 60 views
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 

。如果不匹配,则查询应该显示房间表中的所有房间(即使它在预订表中),只要它们具有不同的日期。

+1

您正在使用'LEFT JOIN预订WHERE booking.RoomID IS NULL' - 只选择没有预订的房间。那么你试图在预订表上搜索?并不是很清楚你用“BETWEEN”来达到什么目的?你能提供一些样本数据和预期结果吗? –

+0

SELECT * FROM 室 左连接预订ON(Room.RoomId = booking.RoomId)GETDATE()BETWEEN bookin.checkindate ' “+ TxtCheckIn.Text +”' AND booking.checkoutdate ' “+ TxtCheckOut.Text +”' “+” ORDER BY Room.RoomType – user1736648

回答

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 
+0

我试过了,但我有错误 '07/25/2013'附近语法不正确。上述

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '07/25/2013'.
是错误我得到了,谢谢,我怎么解决 感谢您的时间,我真的很感激 – user1736648

+0

尝试用硬编码的日期先运行查询,看看它是否工作。该常量不在查询中,因此它是传入的方法。 –