View Single Post
Old 12-12-2011, 12:05 PM  
nation-x
Confirmed User
 
nation-x's Avatar
 
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
First of all, unless you need to display records that exist in the model table that don't have associated records in the other tables... you don't need to use a left join.

INNER JOIN and means to only show records common to both tables. Whether the records are common is determined by the fields in join clause.

LEFT JOIN means to show all records from left table (i.e. the one that precedes in SQL statement) regardless of the existance of matching records in the right table.

RIGHT JOIN means opposite of LEFT JOIN, i.e. shows all records from the second (right) table and only matching records from first (left) table.

SELECT model.id from model
INNER JOIN model_in_city ON (model_in_city.model_id = model.id)
INNER JOIN city ON (city.city_id = model_in_city.city_id)
INNER JOIN province on (city.province_id = province.id)
WHERE province.id = [id for bc];

OR

SELECT model.id from model
INNER JOIN model_in_city ON (model_in_city.model_id = model.id)
INNER JOIN city ON (city.city_id = model_in_city.city_id)
INNER JOIN province on (city.province_id = province.id)
WHERE province.name = 'British Columbia,Quebec';

The design of this database is garbage... but that should work
nation-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote