MySQL & PHP: Joins & Counts
I want to select all users from my table where access_level_id!=0. I also want to count the number of entries in another table where the user id matches a field (this could be 0 or 100).
I have:
SELECT users.id, count(clients.id) as count FROM $db[users] as users
LEFT JOIN $db[clients] as clients 开发者_运维知识库ON users.id=clients.salesrep_id
WHERE users.access_level_id!='0'
This is returning just a single number (17)
You have to group the results. Try adding GROUP BY users.id
to your Query.
精彩评论