Web Hosting Talk







View Full Version : Confused about a MySQL JOIN Query


shockuk
02-02-2009, 05:46 PM
Hi all,
I'm currently making an "attendance register system", but have become stuck whilst using a MySQL join, and was wandering if anyone could help.
Basically, I have two tables...
"Trainees" contains trainee names and information
"Attendance" tracks daily attendance (each row has a week of attendance for each trainee):
=======================
| Trainees |
=======================
| ID | Name | Section |
=======================================
| Attendance |
=======================================
| attID | TraineeID | WeekID | Mon | Tue | Wed |
I'm currently using the following select query, to report back information on the "current week":
SELECT * FROM `trainees` LEFT JOIN `attendance` ON trainees.id = attendance.traineeid WHERE attendance.weekid = '$weekid' AND trainees.section = '$group' ORDER BY `first` ASC
This reports back a list of trainees in the selected group who are listed in "attendance" for the specific "weekID".
My issue arrises because I need the query to report back all of the trainees from the selected group (wether they are located in "attendance" or not), and simply "match up" any trainees that are located in "attendance" with the weekid.
I hope that made sense :)
Would anyone be able to help?
Thanks very much!

foobic
02-02-2009, 08:21 PM
Well, that's what LEFT JOINs are supposed to do, so is it just your WHERE clause that's excluding the trainees with no attendance?
Perhaps try:
WHERE ( ISNULL(attendance.weekid) OR attendance.weekid = '$weekid' ) AND etc.

shockuk
02-03-2009, 05:33 AM
Thanks,
That seems to work like a charm :)
Thanks very much,
Shockuk.