qwave Posted June 3, 2006 Share Posted June 3, 2006 My web form contains a list of valid subjects that user can 'enroll' in.The database contains a table of subjects, and also a table of subjects that users have enrolled in.I need a query that returns a list of subjects that the user has not enrolled in.Subject table: subject_id, nameSubject_Enrolled table: enrollment_id, subject_id, user_idSo I have to return all names in the subject table unless the particular user already has an entry for thesubject in the subject_enrolled table. What is the best way to get the query that returns the names ofall the subjects that the user has not yet enrolled in?Thanks in advance!Grem Quote Link to comment https://forums.phpfreaks.com/topic/11122-simple-sql-query-problem-can-anyone-help/ Share on other sites More sharing options...
fenway Posted June 3, 2006 Share Posted June 3, 2006 A LEFT JOIN would be most appropriate (UNTESTED):[code]SELECT s.name FROM Subject AS sLEFT JOIN Subject_Enrolled AS se USING ( subject_id )WHERE user_id = '<your user id>' AND se.subject_id IS NULL[/code]Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/11122-simple-sql-query-problem-can-anyone-help/#findComment-41585 Share on other sites More sharing options...
qwave Posted June 3, 2006 Author Share Posted June 3, 2006 Brilliant! That seems to have done the trick perfectly. Thanks a bunch! Quote Link to comment https://forums.phpfreaks.com/topic/11122-simple-sql-query-problem-can-anyone-help/#findComment-41588 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.