Jump to content

Help writing this query (joining three tables)


JohnJSal

Recommended Posts

Hi everyone. I'm fairly comfortable with the basics of MySQL, but I'm a little stumped on how to join three tables. First off, let me explain the table structures (from a baseball database):

Master | playerID, nameFirst, nameLast
Schools | schoolID, schoolName
SchoolsPlayers | playerID, schoolID

So the first name in each row is the table name (Master, etc.) and the following names are entries in that table. What I'd like to do is write  a query that will display a player's name and the school he went to. This is what I've come up with so far, but I don't know if it works yet because my script isn't working properly (a separate issue).

select Master.nameFirst, Master.nameLast, Schools.schoolName
from Master, Schools, SchoolsPlayers
where Master.playerID = SchoolsPlayers.playerID and Schools.schoolID = SchoolsPlayers.schoolID

I did a little searching and came across the use of a left or right join as another way to work with more than two tables, but I've never used that before, so I don't know if it's necessary or how it works. It seems like a join is what I need, which the above is not, and I'd like to learn how to write one that uses three (or more) tables. (I can handle two!)

Thanks!
John

Quick note: This does work, but is it better to use joins in a case like this?
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.