0

There are three tables in my database, called profiles, profile_groups & profile_group_members.

profiles contains basic details about employess (user_id,firstname, lastname, email)
profile_groupscontains profile groups such as executives, clerical_staff, support_staff (group_id, group_name).
profile_group_memberscontains members in each groups.(id, group_id, user_id).


Now I want to list users who are not already in a specific group.
For an example : I want to list users who are not already in group_id 2
I found various articles from Stackoverflow but any of them not worked for me.
Please help me to write the code.

Edit : @Paul's code works!!

Lanka
  • 34
  • 8
  • 1
    pseudo code: `select * from profile p where not exists(select * from profile_group_members pg where pg.user_id = p.user_id and pg.group_id = 2)` – Paul Spiegel Feb 27 '17 at 09:12
  • Possible duplicate of [Select rows which are not present in other table](http://stackoverflow.com/questions/19363481/select-rows-which-are-not-present-in-other-table) – Paul Spiegel Feb 27 '17 at 09:15
  • @PaulSpiegel - It works.! Thank you+ – Lanka Feb 27 '17 at 09:33
  • @PaulSpiegel - How do I edit the query if I want to merge the following query with your one? select * from profiles where firstname = '$query' – Lanka Feb 27 '17 at 09:50
  • 1
    `where firstname = '$query' and not exists(...)`. Also have a look at [this Q&A](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) to make your code more secure. – Paul Spiegel Feb 27 '17 at 09:54

0 Answers0