views:

20

answers:

2

I have 3 tables:

  • listing
  • photo
  • calendar

"photo" and "calendar" both have a "listing_id" column in them and every "listing" has a "photo". But I only want to select rows that have no entry in the "calendar" table with the matching "listing_id".

I'm not sure if I'm saying it correctly, but any help would be greatly appreciated. And if someone could show me CodeIgniter syntax, that'd be even better.

A: 

Should work as an SQL query. Not sure about CI syntax. Sorry!

SELECT * FROM listing WHERE listing_id NOT IN (SELECT listing_id FROM calendar)

Dan G
I can't respond directly to your comment, but I'm not quite seeing where free_dates come in. Are you looking for results from the "calendar" table about what's there, or what's not there?
Dan G
+1  A: 

This will produce the list of calendar.free_date values that should not be returned because their associated listing_id values do not exist in the listing table.

select free_date from calendar c 
 where not exists (select * from listing 
                    where listing_id = c.listing_id);
Khorkrak