我想知道是否有人可以幫我查詢我的MySQL數據庫的可用性。如何查詢可用項目?
sooo,我有兩個表:tbl_items(單元項)和tbl_reservations。
tbl_reservations
-barcode
-dateReserved
-timeStart
-timeEnd
tbl_items
-barcode
-itemName
-itemDesc
我想要做的就是這樣的事情(請參考圖片):
到目前爲止,這是我的查詢:
SELECT a.bcode, b.resdate, b.timestart, b.timeend
FROM tbl_items a
INNER JOIN tbl_reservations b
on a.bcode=b.bcode
WHERE a.bcode
not in
(
Select bcode
FROM tbl_reservations
WHERE bcode not in
(
SELECT bcode
FROM tbl_reservations
where resdate='2013-09-25' AND retdate is null
)
AND ((timestart < '10:00'AND timeend < '10:00')
AND (timestart > '15:00' AND timeend > '15:00'))
)
但即使有這樣長的代碼,它仍然允許一些已經預訂或應該不可用的設備出現。
有幫助嗎?
謝謝! :d與我的意思是NOT的保留結果集
截圖被包括在結果:
突出顯示的有,在上面的代碼中重疊的條件的那些...
更新:這裏是新的代碼...我很驚訝,因爲它返回了在10:00之前有timestart和timeend的行。
select rsv.controlno, rsv.bcode, rsv.resdate, rsv.timestart, rsv.timeend
FROM
(
select bcode
from tbl_items
) i
inner join tbl_reservations rsv
on i.bcode=rsv.bcode
WHERE resdate='2013-09-25' AND NOT (rsv.timeend > '10:00' AND rsv.timestart < '15:00');
「它仍然允許一些已經預訂或應該不可用的設備出現。」 - 你能舉一些這樣的例子,這樣我們可以幫助調試嗎?作爲第一步,您可能想在「AND((...)OR(...))'部分加上額外的括號,所以它肯定有正確的優先級。 – Rup
線索:時間是線性的 – Strawberry
結果集和更新的代碼... @Rup .. – user2784251