You need to understand how left / right joins work.
For rows in the
table with no corresponding rows in the
table, all columns from the
table will be
clause will then remove those rows, since
. Neither condition in your
clause will match those rows.
Move your conditions to the
clause instead. NB:
You've already specified the
match, so you don't need to specify that again.
SELECT COALESCE(COUNT(u.`userId`), 0) As 'Total', r.`roleName` As 'Role' FROM `users` u RIGHT JOIN `usertype` r ON u.`userRole` = r.`roleId` And YEAR(u.`userOn`) > 2011 GROUP BY r.`roleName`; NB:
The month and year columns do not make any sense. You're grouping the users by role name, so there could be multiple values for these columns. Which single value are you expecting to appear here? You will need to use an aggregate function to pick the correct one.