Jump to content

Archived

This topic is now archived and is closed to further replies.

qwave

Simple SQL query problem, can anyone help?

Recommended Posts

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, name
Subject_Enrolled table: enrollment_id, subject_id, user_id


So I have to return all names in the subject table unless the particular user already has an entry for the
subject in the subject_enrolled table. What is the best way to get the query that returns the names of
all the subjects that the user has not yet enrolled in?


Thanks in advance!
Grem

Share this post


Link to post
Share on other sites
A LEFT JOIN would be most appropriate (UNTESTED):

[code]SELECT s.name
FROM Subject AS s
LEFT JOIN Subject_Enrolled AS se USING ( subject_id )
WHERE user_id = '<your user id>' AND se.subject_id IS NULL[/code]

Hope that helps.

Share this post


Link to post
Share on other sites
Brilliant! That seems to have done the trick perfectly. Thanks a bunch!

Share this post


Link to post
Share on other sites

×

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.