dbTalk Databases Forums  

SELECT when only a member of one group

comp.databases.mysql comp.databases.mysql


Discuss SELECT when only a member of one group in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Graham Drabble
 
Posts: n/a

Default SELECT when only a member of one group - 03-04-2010 , 02:45 PM






Hi,

I have a db in which I have a users table containing a list of all
the users and a groups table containing a list of all the different
security groups they are in. There is a many:many relationship
between them so I have user_group as a link table between them.

What I want to do is a select in which I retrive those users who are
only a member of one, specified, group.

A simplified table structure is below

users
=====
UID int not null PRIMARY KEY
Fname varchar
Lname varchar

groups
======
GID int not null PRIMARY KEY
GroupName varchar

user_group
==========
GID
UID
(Primary key is a composite of the above)


with sample data

users

================================================== ===================
Quote:
UID | Fname | Lname |
================================================== ===================
1 | Alf | Adams |
2 | Brian | Baker |
3 | Charles | Cole |
================================================== ===================

groups

================================================== ===================
Quote:
GID | GroupName |
================================================== ===================
1 | Apples |
2 | Bananas |
3 | Cherries |
================================================== ===================

user_group

===================
Quote:
UID | GID |
===================
1 | 1 |
1 | 2 |
1 | 3 |
2 | 2 |
3 | 1 |
===================

I need a query to return UID,Fname and Lname from those who are only
a members of group 2 (ie Brian Baker). Getting one to return Alf and
Brian is trivial but I can't see how to return just Brian.

Any pointers?

--
Graham Drabble
http://www.drabble.me.uk/

Reply With Quote
  #2  
Old   
Erick T. Barkhuis
 
Posts: n/a

Default Re: SELECT when only a member of one group - 03-04-2010 , 03:32 PM






Graham Drabble:


Quote:
I need a query to return UID,Fname and Lname from those who are only
a members of group 2 (ie Brian Baker). Getting one to return Alf and
Brian is trivial but I can't see how to return just Brian.

Any pointers?
Take the query you have, use GROUP BY uid
and a clause HAVING COUNT(gid)=1


--
Erick

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.