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
0
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?
0
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
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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
0
LowfatspreadCommented:
SELECT A.*
FROM student A

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

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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.