开发者

MySQL & PHP: Joins & Counts

开发者 https://www.devze.com 2023-03-30 06:41 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消