MYSQL LEFT JOIN and COUNT and GROUP BY

Your 2nd query is fine. Just group by the user from the first table. Otherwise you would turn your left join into an inner join

SELECT
    u.name, 
    COUNT(rh.uid) AS cnt
FROM
    `user` u
LEFT JOIN
    user_rh rh
    ON (rh.uid = u.uid)
GROUP BY
    u.uid, u.name
ORDER BY
    u.name

  1. it does not transform your left join to an inner join. It is just grouping by your uid from user_rh when linked to user. You have 3 different uid (NULL, 2 and 1) that's why you have only 3 lines in your result.

  2. when you are using grouping clause, you can select only field with grouping aggregate (like COUNT, SUM, AVG..) or field available in the group clause. You can omit the grouping but Mysql will do the grouping itself but results can be unreliable since it will choose the first one available. That's why you have Bob displayed in your 3 lines result.