1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

SQL To List Users From Multiple User Groups

Discussion in 'General PHP and MySQL Discussions' started by TheBigK, Oct 15, 2015.

  1. TheBigK

    TheBigK Well-Known Member

    I'm trying to list all the users from various user groups. I can't figure out the SQL to get the list. My best attempt so far is this:
    SELECT * from xf_user WHERE user_group_id IN (3,4,5,6);
    What's the right way to do this? Is there any easy alternative (using inbuilt function)?
  2. TheBigK

    TheBigK Well-Known Member

    Update: I've been trying to get this right and now wondering if I really need to use 'JOIN'. I've a hunch that this is simpler than I think.
  3. borbole

    borbole Well-Known Member

    Try this query:

    SELECT u.username AS 'User',  g.title AS 'User Group'
    FROM xf_user AS u
    LEFT JOIN xf_user_group AS g ON ( u.user_group_id = g.user_group_id ) 
    If you are using a custom prefix for your db tables, then replace xf_ with that prefix in the query above.

Share This Page