John Smith
John Smith

Reputation: 23

Sql Returning null from second select

I have a query like this

select * from Cars where 
car_id!=(Select car_id from reservation where reservation_date between '2013-05-13' and '2013-05-15')

I want to take car_id='' if nothing between that dates but it's not working.

Upvotes: 0

Views: 60

Answers (2)

Dan Bracuk
Dan Bracuk

Reputation: 20804

Using "not in" as per draxxxeus's answer will work, but once you get large amounts of data, your query will be slow. A less intuitive, but logically equivalent way of getting the answer is like this:

where car_id in 
(select car_id
from cars
except 
select car_id
from reservation
etc
)

If your database supports that syntax, it will run faster than using "not in". With some databases, Oracle for example, you have to use the word "minus" instead of "except".

Upvotes: 0

draxxxeus
draxxxeus

Reputation: 1523

First check if this is returning proper values

Select car_id 
   from reservation 
   where reservation_date 
   between '2013-05-13' and '2013-05-15'

Try this:

select * 
from Cars 
where car_id
not in 
(
   Select car_id 
     from reservation 
     where reservation_date between '2013-05-13' and '2013-05-15'
)

Upvotes: 2

Related Questions