Jump to content

The story of 3 little tables


TapeGun007

Recommended Posts

Here are the tables:

 

Table: Members

Member_ID

FirstName

LastName

 

Table: Departments

Dept_ID

Dept_Name

 

Table: Department_Members

DM_ID

Dept_ID

Member_ID

 

I want to be able to spit out the Dept_ID, Dept_Name, Member_ID, FirstName, LastName.

 

I just did a JOIN, but now this is kind of a 3 way join, is that possible?

Link to comment
Share on other sites

SELECT Departments.Dept_ID, Departments.Dept_Name, Department_Members.Dept_ID, Department_Members.MemberID, Department_Members.DM_ID, Members.FirstName, Members.LastName
FROM Departments, Department_Members, Members
WHERE Departments.Dept_ID = Department_Members.Dept_ID
AND Members.MemberID = Department_Members.MemberID

 

I did this and it works, but... is this really the correct way?

 

I made a small mistake on the original example, MemberID isn't "Member_ID".

Link to comment
Share on other sites

It's almost the "proper" way, but MySQL will likely optimize it in the right direction for you:

SELECT ...
FROM Departments
JOIN Department_Members ON Departments.Dept_ID = Department_Members.Dept_ID
JOIN Members ON Department_Members.MemberID = Members.MemberID

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.