MySQL query to return users in specific groups

Graham Horsman June 23, 2014

I'm banging my head against this. I'm pretty sure it's simple but my MySQL is pretty rusty. What I'd like to do is return usernames, full name email address and group memeberships for users who are both in "jira-login" and in another one of a list of groups. Example:

davebloggs is in jira-login and client1 and his details should be in the results

fredbloggs is in client1 but not in jira-login his details should be excluded

joebloggs is in client2 and jira-login and his details should be returned

billbloggs is in jira-login only and his details should be excluded

Can anyone help?

4 answers

9 votes
Tiago Comasseto
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 23, 2014

Hi Graham, you can retrieve the list of members of a group with the following query:

select * from cwd_membership where parent_name = '<group_name>';

Then, you can run another query like this to return the user informtion:

select * from cwd_user where lower_user_name in (select child_name from cwd_membership where parent_name = '<group_name>')

I hope it helps.

Cheers

Nithya Ananda August 25, 2016

Thank you!

2 votes
crf
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 24, 2014

If you have "Nested Groups" enabled, this isn't going to be easy. However, if you do not mind getting only the direct members and that you will also get answers for disabled directories, I think you want something like this:

SELECT cu.user_name, cu.display_name, cu.email_address
    FROM cwd_user AS cu
    INNER JOIN cwd_membership AS cm
        ON cu.directory_id=cm.directory_id
        AND cu.lower_user_name=cm.lower_child_name
        AND cm.membership_type='GROUP_USER'
    WHERE cm.lower_parent_name='jira-login';

crf
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 24, 2014

Sorry, that gets all of those in jira-login... rereading your requirements, add to this something like:

AND EXISTS ( SELECT 1 FROM cwd_membership x
            WHERE x.directory_id=cu.directory_id
            AND x.lower_child_name=cu.lower_user_name
            AND x.lower_parent_name &lt;&gt; 'jira-login' )

It's a bit gross, but should get you there.

Graham Horsman June 24, 2014

Thanks very much. One question though. If I want to return a list of users who are in both jira-login AND "client1" where do I define "client1"?

crf
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 25, 2014

The last line would become

AND x.lower_parent_name = 'client1' )

to be more specific about the name of the second group.

Graham Horsman June 30, 2014

Apologies for the late reply. I've tried this but it's returning users who are not in the group specified in "ANDx.lower_parent_name = 'client1')"

0 votes
Eva Fernández October 19, 2016


How I can know that users are only included in a group?

 

Thanks!!

Eva

0 votes
Paula Manildi January 10, 2016

I need this for Confluence... if anyone has it.

Suggest an answer

Log in or Sign up to answer