It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

MySQL query to return users in specific groups

Graham Horsman Jun 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

5 votes
Tiago Comasseto Jun 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.


Nithya Ananda Aug 25, 2016

Thank you!

2 votes
Chris Fuller Atlassian Team Jun 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';

Chris Fuller Atlassian Team Jun 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 Jun 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"?

Chris Fuller Atlassian Team Jun 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 Jun 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
Paula Manildi Jan 10, 2016

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

0 votes
Eva Fernández Oct 19, 2016

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




Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

4,321 views 21 23
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you