Jump to content

Simple SQL query problem, can anyone help?


qwave

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
Link to comment
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.
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.