![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
![]() I'm trying to list all the models listed in the province British Columbia. Here's my tables:
Code:
Table: model column: id = 1,2 Table: city column: city_id = 33,34 column: city_name = Vancouver,Victoria column: province_id = 9,9 Table: model_in_city column: model_id = 1,2 column: city_id = 33,34 Table: province column: id = 9,10 column: name = British Columbia,Quebec model.id = model_in_city.model_id city.city_id = model_in_city.city_id city.province_id = province.id WHERE province.name = British Columbia I tried the following query but can't seem to LEFT JOIN 2 tables? Code:
SELECT id from model LEFT JOIN model_in_city ON (model_in_city.model_id = model.id) LEFT JOIN city ON (city.province_id = province.id); #1054 - Unknown column 'province.id' in 'on clause' |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
Confirmed User
Industry Role:
Join Date: Sep 2006
Location: Netherlands
Posts: 217
|
You can join as many tables as you want... but, just like MySQL said, it doesn't know the column province.id.. Mainly because you haven't included the table called 'province' in your query I guess.
![]()
__________________
Dido ADAMO Advertising - Your ULTIMATE traffic partner! If you need traffic or have traffic, we'd love to help you make the best out of it! ICQ:24209500 - Skype:diederikvanschaik |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 |
Confirmed User
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
|
You are missing the join for province.
__________________
Mechanical Bunny Media Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
Confirmed User
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 |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
That worked thanks nation-x.
The table model_in_city is picking up multiple results for model.id in British Columbia because they can be shown in multiple cities but only want to display them once in British Columbia. For example: Code:
Table: model_in_city column: model_id = 1 column: city_id = 33 column: city_display = 1 column: model_id = 1 column: city_id = 34 column: city_display = 2 |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
Will I have to create a new column under the table model_in_city named province_id then
run a UNION and SELECT DISTINCT province_id FROM model_in_city ? |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
Figured it out, thanks!
|
![]() |
![]() ![]() ![]() ![]() ![]() |