benphp Posted March 11, 2011 Share Posted March 11, 2011 I have 3 tables: classes CID|Cdate students SID|LastName|FirstName classes_students CLID|SID|CID I'm trying to get the students who have NOT attended a class since 2009-01-01. I've managed to kludge together something with if statements, but I'd like to know how to do it properly in SQL. I can write the SQL to get positive results, but I can't figure out how to get negative results. Any help is appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/230285-how-to-get-students-who-have-not-attended/ Share on other sites More sharing options...
fenway Posted March 11, 2011 Share Posted March 11, 2011 LEFT JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/230285-how-to-get-students-who-have-not-attended/#findComment-1185958 Share on other sites More sharing options...
benphp Posted March 11, 2011 Author Share Posted March 11, 2011 I can get the positive - that is, students who do have a record after a certain date. Select DISTINCT students.SID, students.LastName, students.FirstName FROM classes_students LEFT JOIN classes ON (classes_students.CLID=classes.CLID) LEFT JOIN students ON (classes_students.SID=students.SID) WHERE classes.Cdate > '2009-01-01' However, I can't figure out how to get students who have NO record for dates beyond 2009-01-01. Quote Link to comment https://forums.phpfreaks.com/topic/230285-how-to-get-students-who-have-not-attended/#findComment-1186197 Share on other sites More sharing options...
benphp Posted March 11, 2011 Author Share Posted March 11, 2011 I think I figured it out. Select DISTINCT students.SID, students.LastName, students.FirstName FROM classes_students as cs LEFT JOIN classes ON (cs.CLID=classes.CLID) LEFT JOIN students ON (cs.SID=students.SID) WHERE students.EID NOT IN (SELECT DISTINCT classes_students.EID FROM classes_students LEFT JOIN classes ON (students.CLID=classes.CLID) WHERE classes.CDate > '2010-01-01' ) ORDER BY students.LastName ASC Quote Link to comment https://forums.phpfreaks.com/topic/230285-how-to-get-students-who-have-not-attended/#findComment-1186218 Share on other sites More sharing options...
fenway Posted March 11, 2011 Share Posted March 11, 2011 You needed "IS NULL", not a NOT IN. Quote Link to comment https://forums.phpfreaks.com/topic/230285-how-to-get-students-who-have-not-attended/#findComment-1186293 Share on other sites More sharing options...
benphp Posted March 11, 2011 Author Share Posted March 11, 2011 It seems to work though. How would you use IS NULL? I tried a few different ways but got nothin Quote Link to comment https://forums.phpfreaks.com/topic/230285-how-to-get-students-who-have-not-attended/#findComment-1186425 Share on other sites More sharing options...
kickstart Posted March 11, 2011 Share Posted March 11, 2011 Hi Using NOT IN is less efficient, but in this case also unnecessary. Select students.SID, students.LastName, students.FirstName FROM students LEFT OUTER JOIN classes_students AS cs ON cs.SID=students.SID LEFT OUTER JOIN classes ON (cs.CLID=classes.CLID AND classes.CDate > '2010-01-01' ) WHERE classes.CLID IS NULL ORDER BY students.LastName ASC All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/230285-how-to-get-students-who-have-not-attended/#findComment-1186452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.