2017-05-26 47 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) 

首先它检查所有没有租金的车。然后有可能汽车返回并再次租用。所以要获得只有可用的汽车,是通过添加不存在的(query_to_search_car_is_on_rent)子句来实现的。

+0

那就是我在找什么。谢谢 –

+0

你可以标记答案是正确的。让其他人受益。 –

0

您应该检索只有startdate存在的汽车。

Select * from Rental where rentenddate is null. 
+0

如果出租日期为null,则此车尚未退回。 –