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 > Data Access, Manipulation & Batch Languages > ANSI SQL > How to join three tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-30-02, 11:04
s.selland s.selland is offline
Registered User
 
Join Date: Feb 2002
Posts: 10
How to join three tables

Hi All

I'm having some problems with query that needs to obtain values from two tables which has an m:n relationship. First I'll just lay out the tables so that you can better understand my problem.

---------------------
table Articles

ArticleID PK
Heading
Intro
Text
Category
---------------------

---------------------
table ImageUsers

ArticleID PK
ImageID PK
Position
----------------------

----------------------
table Images

ImageID PK
ImageURL
----------------------

As you can see the tables represents articles which can have one or more images associtated with them.

I need to run a query that will return the following information for all entries in table "Articles":

Article.ArticleID, Article.Heading, Article.Intro, Article.Text, Article.Category, ImageURL.

ImageURL should only have a value if the article has an entry in table "ImageUsers" and only if ImageUsers.Position = 'small', if not ImageURL in the recordset should be empty, or contain a null value.

I would also like to limit the result to articles belonging to a certain category.

I've tried different variations of joins and subqueries but I just can't seem to come up with a query that will return the correct values.

All help will be appreciated

Regards

Stian
Reply With Quote
  #2 (permalink)  
Old 04-30-02, 11:37
alligatorsql.com alligatorsql.com is offline
Registered User
 
Join Date: Jul 2001
Location: Germany
Posts: 189
Hello,

what about ...

SELECT a.articleid, a.heading, a.intro, a.Text, a.categroy, i.imageurl
FROM articles a, imageusers iu, images i
WHERE a.articleid = iu.articleid
AND iu.imageid = i.imageid

the result: all articles that have at least one image

Hope this helps

Manfred Peter
(Alligator Company)
http://www.alligatorsql.com
Reply With Quote
  #3 (permalink)  
Old 04-30-02, 11:43
s.selland s.selland is offline
Registered User
 
Join Date: Feb 2002
Posts: 10
That works but...

I also need to return those articles that does not have any images at all. I guess I could run a separate query to do this, but I'd rather not.

Cheers

Stian
Reply With Quote
  #4 (permalink)  
Old 04-30-02, 11:57
alligatorsql.com alligatorsql.com is offline
Registered User
 
Join Date: Jul 2001
Location: Germany
Posts: 189
Hello again,

than you need an outer join :

SELECT a.articleid, a.heading, a.intro, a.Text, a.categroy, i.imageurl
FROM articles a, imageusers iu, images i
WHERE a.articleid = iu.articleid(+)
AND iu.imageid = i.imageid(+)

This works for Oracle ... Which database are you using ?

Greetings
Manfred Peter
(Alligator Company)
http://www.alligatorsql.com
Reply With Quote
  #5 (permalink)  
Old 04-30-02, 13:09
s.selland s.selland is offline
Registered User
 
Join Date: Feb 2002
Posts: 10
Hi Again

Currently I'm using a MS Access database, but will be moving to mySQL in the near future

Stian
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