How to count rows in a joined table

I have 2 tables - master (called groups) and details (called by users)

I want to return each master line for later display, but I only want to count how many detail lines match the master

My query is not working correctly - it only returns the first row in the main table

$query_string = '
    SELECT groups.userGroupID, userGroup,
           count(users.userGroupID) AS howMany
    FROM groups_table AS groups
    JOIN users_table AS users ON users.userGroupID = groups.userGroupID
    ORDER BY groups.userGroupID
';

      

Thanks for the help.

+3


source to share


1 answer


Forgot my group:



SELECT groups.userGroupID, userGroup,
       count(users.userGroupID) AS howMany
FROM groups_table AS groups
LEFT JOIN users_table AS users ON users.userGroupID = groups.userGroupID
GROUP BY groups.userGroupID
ORDER BY groups.userGroupID

      

+7


source







All Articles