sql - How can I return a row if its ID is not found in another table? -


i have 2 tables in ms sql 2008 database, listings , listingtype, want create select statement give me rows listing not have listingid in listingtype table.

i'm confused how start statement.

example sql statement - lot more explained, should able i'm asking it.

select     listing.title, listing.mls, coalesce (pictures.pictureth, '../default_th.jpg') pictureth, coalesce (pictures.picture, '../default.jpg') picture, listing.id,                        listing.description, listing.lot_size, listing.building_size, listing.bathrooms, listing.bedrooms, listing.address1, listing.address2,                        listing.city, locations.abbrev, listing.zip_code, listing.price, listing.year_built, listingtypematrix.listingtypeid         listing inner join                       locations on listing.state = locations.locationid left outer join                       listingtypematrix on listing.id = listingtypematrix.listingid left outer join                       pictures on listing.id = pictures.listingid     (listingtypematrix.listingtypeid = '4') ,            ((pictures.id null) or (pictures.id =                           (select     min(id)                                      pictures                                  (listingid = listing.id)))) 

listingtypematrix.listingtypeid = '4' part dont know change to, because there not record it.

using not exists

select t.*   listing t  not exists(select null                     listingtype lt                    lt.listingid = t.listingid) 

using not in

select t.*   listing t  t.listingid not in (select lt.listingid                              listingtype lt) 

using left join/is null

   select t.*      listing t left join listingtype lt on lt.listingid = t.listingid     lt.listingid null 

summary

quote:

in sql server, not exists , not in predicates best way search missing values, long both columns in question not null. produce safe efficient plans kind of anti join.

left join / null less efficient, since makes no attempt skip matched values in right table, returning results , filtering them out instead.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -