2013-07-25 91 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

嘗試用硬編碼的日期先運行查詢,看看它是否工作。該常量不在查詢中,因此它是傳入的方法。 –