Jump to content

Simple JOIN help needed


benphp

Recommended Posts

Hi, this will be easy for anyone who knows SQL better than I.

 

I have 4 tables.

 

tblCourses

tblClasses

tblClass_Students

tblEmployee

 

There's a ClassID, CourseID, EID that links these together, and I can get a list of classes and dates using this SQL:

 

Select DISTINCT
tblClasses.ClassDate,  
tblClasses.ClassID,
tblClass_Students.EID
FROM tblClasses
LEFT OUTER JOIN tblCourses ON (tblClasses.CourseID=tblCourses.CourseID)
LEFT OUTER JOIN tblClass_Students ON (tblClasses.ClassID=tblClass_Students.ClassID)
Where  
tblCourses.SUID = $SUID
AND
tblCourses.CourseID = $temp
AND
tblCourses.Type_10 = '$active'
ORDER BY tblClasses.ClassDate DESC

 

How can I get the employee names - tblEmployee.Last, tblEmployee.First?

 

Or do I have to write a query within the query results?

 

Thanks!

 

B

 

 

Link to comment
Share on other sites

I tried this:

 

Select DISTINCT
   tblClasses.ClassDate, 
   tblClasses.ClassID,
   tblClass_Students.EID,
   tblEmployee.Last
FROM tblClasses
LEFT OUTER JOIN tblCourses ON (tblClasses.CourseID=tblCourses.CourseID)
LEFT OUTER JOIN tblClass_Students ON (tblClasses.ClassID=tblClass_Students.ClassID)
LEFT OUTER JOIN tblEmployee ON (tblCourses.EID=tblEmployee.EID)
Where 
   tblCourses.SUID = $SUID
AND
   tblCourses.CourseID = $temp
AND
   tblCourses.Type_10 = '$active'
ORDER BY tblClasses.ClassDate DESC

 

But it doesn't work - the syntax is wrong - probably because I'm not joining the select table -  and I don't know SQL well enough to know how to stitch it together. How would you write that join?

 

Link to comment
Share on other sites

Hi

 

Nothing jumps out. What is the error you are getting?

 

By the way, as you are checking values from tblCourses in the WHERE clause there is no point to using a LEFT OUTER JOIN. That would get rows where there is no matching row on tblCourses which the WHERE clause would then throw away.

 

All the best

 

Keith

Link to comment
Share on other sites

Keith - thanks! It was a table naming problem  ::)

 

Your confirmation that it looked okay made me look at other issues.  This one worked:

 

Select DISTINCT
tblClasses.ClassDate,  
tblClasses.ClassID,
tblClass_Students.EID,
tblEmployee.Last,
tblEmployee.First,
tblClass_Students.Grade
FROM tblClasses
LEFT OUTER JOIN tblCourses ON (tblClasses.CourseID=tblCourses.CourseID)
LEFT OUTER JOIN tblClass_Students ON (tblClasses.ClassID=tblClass_Students.ClassID)
LEFT OUTER JOIN tblEmployee ON (tblClass_Students.EID=tblEmployee.EID)
Where  
	tblClasses.CourseID = $CourseID
AND
	tblClass_Students.Grade LIKE 'P%'
$ORDERBY  

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.