简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM