Jump to content

Join three tables?


Christopher.Burkhouse
 Share

Recommended Posts

Hey guys. I'm relearning Joins (I'm trying to finally delve deeper into this section for obvious reasons), and wondering if I can join three tables together, and how. W3schools goes over Joins very well, but never covers anything of this matter. Here's a small sample of what my database would look like.

 

Usernames

id - username - pic

1 - User1234 - pic.jpg

2 - User4321 - pict.jpg

3 - Usernam1 - p1ct.jpg

 

Websites

id - name - url

1 - webna - webna.c

2 - webur - webur.c

3 - websi - websi.t

 

Tickets

id - user_id - website_id

1 - 1 - 2

2 - 3 - 2

3 - 3 - 2

 

Pretty much I'd want to pull from the tickets table. I want to include...

tickets.id, usernames.username, usernames.pic, websites.name, websites.url

 

But not really sure where to go from here. Currently when a ticket is created, I have it store the user id, website name, and website url, to only have to join one file. This leads to redundancy and possible inconsistencies

 

Thanks,

Chris

Link to comment
Share on other sites

You can join as many tables as you'd like. When you join tables you build a new temporary table in memory, so you can keep joining more tables to that temporary table as long as you have enough memory to hold the joined result. In order to make that faster and more efficient you should specify the join conditions when you do the join using ON.

SELECT tickets.id, usernames.username, usernames.pic, websites.name, websites.urlFROM ticketsJOIN usernames ON tickets.user_id = usernames.idJOIN websites ON tickets.website_id = websites.id
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...