Jump to content

Archived

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

JohnJSal

Help writing this query (joining three tables)

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?

Share this post


Link to post
Share on other sites
If you know how to do it with 2, you can do it with N -- but try rewriting it in proper JOIN syntax, and the ON clauses will make life simpler for you.

Share this post


Link to post
Share on other sites

×

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.