2017-05-26 7 views
0

利用可能な車のクエリを下の欄に書き込む必要があります。簡易テーブルのクエリ

enter image description here

私はこの試みた:

select * 
from [Rental] 
where [Rental].RentEndDate is not null 

enter image description here

をしかし、それはすでに借りた車— CarID 5はレンタル可能であってはならない示しています。これをどうすれば解決できますか?

答えて

0

これを試してください。

Select * from Rental r where r.rentenddate is not null 
and not exists (select 1 from rental r1 where r1.carid = r.carid and r1.rentenddate is null) 

まず、家賃のない車をすべてチェックしています。その後、車が返却され、再びレンタルされる可能性があります。したがって、利用可能な車のみを取得するには、not exists(query_to_search_car_is_on_rent)節を追加します。

+0

thats私は何を探しています。ありがとう –

+0

あなたは正しいと答えることができます。他の人に恩恵を受けることができます。 –

0

出発日のみが存在する車を検索する必要があります。

Select * from Rental where rentenddate is null. 
+0

rentdateがnullの場合、この車はまだ復帰していません。 –