Jump to content

SQL query on 3 tables


ja_blackburn

Recommended Posts

I have 3 tables in the following structure:

 

Table: main_event

 

Field: user_id

Field: main_event_id [PK]

 

Table: sub_event

 

Field: main_event_id

Field: sub_event_id [PK]

 

Table: sub_event_entered

 

Field: sub_event_entered_id [PK]

Field: sub_event_id

Field: race_no

 

My aim is to perform the following query but I keep getting stuck linking the 3 tables together:

 

SELECT sub_event_id, race_no FROM sub_event_entered WHERE user_id = $user_id

 

What is the correct syntax of this statement linking the tables?

 

Thanks!

Link to comment
Share on other sites

SELECT sub_event_entered.sub_event_id, sub_event_entered.race_no 
FROM sub_event_entered  LEFT JOIN sub_event 
ON (sub_event_entered.subevent_id = sub_event.subevent_id)
LEFT JOIN main_event 
ON (sub_event.main_event_id = main_event.main_event_id)
WHERE main_event.user_id = $user_id

 

JOIN is your best friend - take the time to learn it well.

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.