If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
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, 20: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, 21:02
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 06-03-08, 22:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
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
please visit Simply SQL and buy my book
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

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