sql - Selecting data not matching query -


i want select data not match query.

say have 2 tables:

cars:

id         int          primary key identity make       varchar(255) not null model      varchar(255) not null pricegroup varchar(1)   not null 

rentals:

id         int          primary key identity sdate      date         not null edate      date         not null car_id     int          not null 

sdate in rentals starting date of rental - edate end date.

i want find available cars between specified time period.

this query works:

select * cars id not in    (select car_id rentals sdate <='2013-04-28' , edate >='2013-04-30') 

however, works specific dates. if enter period between 2013-04-25 , 2013-05-30 cars shows up, though of them booked in between period.

i use ms sql 2012.

you need compare dates other way around:

select * cars id not in    (select car_id rentals sdate <='2013-04-30' , edate >='2013-04-28') 

also, using not exists may give better performance not in:

select * cars not exists   (select 1 rentals car_id=cars.id , edate>=start_date , sdate<=end_date) 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -