ja_blackburn Posted February 23, 2011 Share Posted February 23, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/228586-sql-query-on-3-tables/ Share on other sites More sharing options...
Muddy_Funster Posted February 23, 2011 Share Posted February 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228586-sql-query-on-3-tables/#findComment-1178590 Share on other sites More sharing options...
ja_blackburn Posted February 23, 2011 Author Share Posted February 23, 2011 Thanks - will do Quote Link to comment https://forums.phpfreaks.com/topic/228586-sql-query-on-3-tables/#findComment-1178621 Share on other sites More sharing options...
Muddy_Funster Posted February 23, 2011 Share Posted February 23, 2011 No problem, let us know how you get on. Quote Link to comment https://forums.phpfreaks.com/topic/228586-sql-query-on-3-tables/#findComment-1178624 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.