Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos 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 contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > joining multiple tables?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-03-08, 21:44
stephank stephank is offline
Registered User
 
Join Date: Aug 2007
Posts: 16
joining multiple tables?

Hello Forum


I have the following situation:

IMAGES Table
PEOPLE Table
CATEGORIES Table
CATEGORIES_PEOPLE Table
PEOPLE_IMAGES Table

Is there a way to retrieve all people including their images from one category with one mySQL statement?

I have tried the following but it doesn't work"

Any insight appreciated.

Thanks!

stephank

-----------------------------------------

SELECT CATEGORIES_PEOPLE.*,
CATEGORIES.*,
PEOPLE.*,
PEOPLE.PersonName, PEOPLE.Position, PEOPLE.Bio, PEOPLE.Statement,
IMAGES.Path,IMAGES.ID

FROM CATEGORIES_PEOPLE, PEOPLE_IMAGES

INNER JOIN PEOPLE ON CATEGORIES_PEOPLE.PEOPLEID = PEOPLE.ID
INNER JOIN CATEGORIES ON CATEGORIES_PEOPLE.CategoryID = CATEGORIES.ID
INNER JOIN IMAGES ON PEOPLE_IMAGES.IMAGESID = IMAGES.ID

WHERE CATEGORIES_PEOPLE.CategoryID = '1'

ORDER BY CATEGORIES_PEOPLE.OrderNum
Reply With Quote
  #2 (permalink)  
Old 06-03-08, 22:02
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 5,444
__________________
Warning
May! contain traces of NUT. people with NUT allergies should not pay attention to any of the above
Reply With Quote
  #3 (permalink)  
Old 06-03-08, 23:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,539
Quote:
Originally Posted by stephank
FROM CATEGORIES_PEOPLE, PEOPLE_IMAGES

INNER JOIN PEOPLE ON CATEGORIES_PEOPLE.PEOPLEID = PEOPLE.ID
INNER JOIN CATEGORIES ON CATEGORIES_PEOPLE.CategoryID = CATEGORIES.ID
INNER JOIN IMAGES ON PEOPLE_IMAGES.IMAGESID = IMAGES.ID
please, don't do that

you are mixing the old style comma joins with the new style JOIN syntax

first of all, if you already know which category (based on your WHERE clause), then you do not need the CATEGORIES table at all

this also means you should start your joins at the CATEGORIES_PEOPLE table

Code:
FROM categories_people INNER JOIN people ON people.id = categories_people.peopleid INNER JOIN people_images ON people_images.peopleid = people.id INNER JOIN images ON images.id = people_images.imagesid WHERE categories_people.categoryid = 1
please note carefully the sequence of tables in the joins

also, note that if categoryid is numeric, you want to compare it to the number 1, not the string '1'
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On