MySQL: SELECT from table if values don't exists in other table

I have two MySQL tables - student and groupmember. Both tables have the field sid.

I want to create a query that will select from the student table if the value doesn't exist in the groupmember table.
There is also a groupmember.grpid field which I will need to match on.

Some pseudo code would look like this:

SELECT ALL STUDENTS WHERE NO RECORD IN groupmember HAS VALUES groupmember.sid=1 AND groupmember.grpid=2

obviously the student.sid and groupmember.sid will need to be joined to ensure only those not in the groupmember table exist.

Thanks
LVL 8
jaxstormAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

UnifiedISCommented:
SELECT A.*
FROM student A
LEFT OUTER JOIN groupmember B
ON A.sid = B.sid
AND A.grpid = b.grpid
WHERE B.sid is null
jaxstormAuthor Commented:
Hi unified, I don't think I've explained very well because I'm not sure that solution will work.

The students are entered into the groupmember table along with a grpid, the grpid doesn't exist in the students table.

Students can exist in more than one group, so I need the query to match exactly based on the grpid (which is passed from a PHP variable)

Any ideas?
jaxstormAuthor Commented:
I have managed this with a PHP solution rather than going the MySQL route.

However, I'm interested to know if this is possible via MySQL so I'll leave the question open for a day and see what people come up with.

Thanks
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

UnifiedISCommented:
I think this is what you want then:

SELECT A.*
FROM student A
LEFT OUTER JOIN groupmember B
ON A.sid = B.sid
AND B.grpid = your variable
WHERE B.sid is null
LowfatspreadCommented:
SELECT A.*
FROM student A

WHERE not exists (select sid from groupmember B
where  A.sid = B.sid
AND b.grpid = 2
)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jaxstormAuthor Commented:
Yep, that worked brilliantly
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.