Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:56:32+00:00 2026-05-27T20:56:32+00:00

I am trying to pull out the information about each user group, while counting

  • 0

I am trying to pull out the information about each user group, while counting the number of users in each.
The counting works, however if a group does not contain any users, it does not retrieve the information – it simply gets ignored.

This is my query:

 SELECT
       g.*,
       count(u.username) as total,
       u.usergroup
  FROM
      usergroups as g,
      users as u
  WHERE
      u.usergroup = g.g_id
  GROUP BY
      g.group_name

This is a part of my “users” and “user groups” tables:

 Sample usergroups table structure
  +--------------------------+---------------+
  | Field                    | Type          |
  +--------------------------+---------------+
  | g_id                     | int(10)       |<- the user group's ID.
  | group_name               | varchar(255)  |<- the name of the user group
  +--------------------------+---------------+

  Sample users table structure
  +--------------------------+---------------+
  | Field                    | Type          |
  +--------------------------+---------------+
  | id                       | int(10)       |<- the user ID
  | username                 | varchar(255)  |<- the username
  | usergroup                | int(10)       |<- ID of the user group the user is in
  +--------------------------+---------------+

I have no idea what I have missed – but then again, I am very new to more complex queries (if you can call it that).

Any help would be very appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:56:32+00:00Added an answer on May 27, 2026 at 8:56 pm

    You want a left join, and you’re doing an inner join:

     SELECT
           g.*,
           count(u.username) as total,
           u.usergroup
      FROM
          usergroups as g
          left join users as u on
               g.u_id = u.usergroup
      GROUP BY
          g.u_id,
          g.group_name
    

    Now that I have your attention, I’d like to take a minute to discuss what join conditions are. See, when you’re first learning MySQL, they teach you the syntax you use. And hey, it works great, for certain sets of problems. Unfortunately, it causes you to equate “join condition” with “where predicate,” and that’s a problem.

    In this case, we’re saying, “Hey, MySQL, grab me all the stuff out of the usergroups table, and try to find a match to the users table where the u_id column from usergroups equals the usergroup column from users. If you can’t find anything, that’s okay, bring the usergroup back anyway, and just null out the columns that users brings to the table.”

    Previously, you were saying, “Hey, MySQL, grab me all the stuff out of the usergroups and the users table, but only where the u_id column from usergroups matches the usergroup column from users.”

    If you had done it with inner join syntax, like so:

     SELECT
           g.*,
           count(u.username) as total,
           u.usergroup
      FROM
          usergroups as g
          inner join users as u on
               g.u_id = u.usergroup
      GROUP BY
          g.u_id,
          g.group_name
    

    You would have been saying, “Hey, MySQL, grab me everything from the usergroups table, and then go find everything in the users table where u_id equals usergroup. If you don’t find a match, I don’t want to see that usergroup show up in the results.

    So, to be clear, the where predicates filter the result set, whereas the join conditions map one table to another.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, I'm pretty much trying to figure out how to pull information from a
I am trying to parse an XML file I made and pull information out
I'm trying to pull out information from a database with PHP and have the
I am trying to pull a page for parsing information out of it using
I am trying to pull out data from the table I had from the
im having some difficulty trying to pull out a specific value from a cookie.
I am trying pull a field out of a string and return it. My
Trying to parse an SQL string and pull out the parameters. Ex: select *
I'm trying to complete a regular expression that will pull out matches based on
I'm trying to use Ajax to fetch an HTML page and then pull out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.