我有這樣的Sql返回null從第二選擇
select * from Cars where
car_id!=(Select car_id from reservation where reservation_date between '2013-05-13' and '2013-05-15')
我想借此car_id =「」如果這之間沒有任何東西日期的查詢,但它不工作。
我有這樣的Sql返回null從第二選擇
select * from Cars where
car_id!=(Select car_id from reservation where reservation_date between '2013-05-13' and '2013-05-15')
我想借此car_id =「」如果這之間沒有任何東西日期的查詢,但它不工作。
首先檢查,如果這是返回正確的價值觀
Select car_id
from reservation
where reservation_date
between '2013-05-13' and '2013-05-15'
試試這個:
select *
from Cars
where car_id
not in
(
Select car_id
from reservation
where reservation_date between '2013-05-13' and '2013-05-15'
)
謝謝你的工作 –
@BarışSaçıkara。 。 。如果這個答案適合你,那麼你應該接受它。 –
「不是」按draxxxeus的答案將工作中使用,但一旦你得到大量的數據,你的查詢會很慢。得到答案的一個不太直觀,但在邏輯上等同的方法是這樣的:
where car_id in
(select car_id
from cars
except
select car_id
from reservation
etc
)
如果你的數據庫支持的語法,它將運行比使用「不是」得更快。對於一些數據庫,例如Oracle,您必須使用「minus」而不是「except」。
。 。 「不在」的性能取決於數據庫。即使在通常表現不佳的數據庫中,通常對於正確的索引也是非常有效的。 –
你在說什麼SQL引擎?試試'NVL(subquery,'')' –