Jump to content

help with query


mottwsc

Recommended Posts

I have two tables:

 

Events
  EventType                               VARCHAR(10)
  InterestID1                             INT
  InterestID2                             INT
  InterestID3                             INT

Interests
  InterestID                              INT
  InterestDescription                     VARCHAR(20)

 

I'm trying to query both tables so that, for each row in Events, I can get all fields from Events and the corresponding interestDescription from Interests for each of the three interests (InterestID1, ID2 and ID3). 

 

This would output something like:

EventType  InterestID1  InterestDescription (for ID1)  InterestID2  InterestDescription (for ID2)  InterestID3  InterestDescription (for ID3)

Car Show  3  Engines  6  Interiors  17  Fuel Efficiency

 

However, I can't do this with a query like:

SELECT *, i.interestDescription FROM EVENTS e
JOIN Interests i on 
(e.interestID1 = i.interestID or e.interestID2 = i.interestID or e.interestID3 = i.interestID)

 

Can anyone suggest how to do this?

 

Thanks!

Link to comment
Share on other sites

SELECT e.*

    , i1.interestDescription AS i1_interestDescription

    , i2.interestDescription AS i2_interestDescription

    , i3.interestDescription AS i3_interestDescription

  FROM EVENTS e

LEFT OUTER

  JOIN Interests AS i1

    ON i1.interestID = e.interestID1

LEFT OUTER

  JOIN Interests AS i2

    ON i2.interestID = e.interestID2

LEFT OUTER

  JOIN Interests AS i3

    ON i3.interestID = e.interestID3

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.