MySQL query to return users in specific groups

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

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

1 vote
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.

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.

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')"

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


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

 

Thanks!!

Eva

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

21,502 views 2 7
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you