Skip to content
Advertisement

How to do SQL Join with many tables (FK tables have looped results sharing ID)

I am newish to SQL and Join statements and I am way out of my league at the moment I currently have 6 Database Tables that are all linked to the main 7th table based on the main tables ID, however all the information in the other 6 tables are looped and so have several displayed results to the one main tables ID.

Is it possible to join them all into one Join Statement so I can have a results so that everyones information from the main table also shows their information from the 6 other linked tables

So basically when they all have the informationed joined I want to be able to Display all information on a webpage so I was wondering do I need to do multiple JOIN statements or just one Longer one?

I have Included some Images below that explain it visually. See examples 1 and 2

  1. The columns that are highlighted in yellow are looped to have many results:

Database Tables Relational link 2. This is the example of how the information is looped into the database where there are many Race_id sharing to the same inf_id:

Example of the Loop From History Table

Im not so sure how it will look once it has been joined since some of the information is looped into many Id’s and not sure if that means it need to duplicate the column or the rows?? any help would be greatly appreciated.

Advertisement

Answer

You could use left join eg for the first tables influencer, social, activities

select i.*,   s.follower, s.Social_Medial_URL, a.activity, a.result 
from influencer  i 
left join  social s on s.inf_id = i.id 
left join  activities a on  a.inf_id = i.id 

you can procede yourself adding the left join for the others tables using the same rules

 select i.*
  , s.follower_count
  , s.social_media_url
  , a.compete_activity
  , a.compete_results 
from influencers i 
left join inf_other_social s on s.inf_id = i.id 
left join inf_compete_activity a on a.inf_id = i.id 
LIMIT 0, 25 
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement