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
https://forums.phpfreaks.com/topic/228586-sql-query-on-3-tables/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.