This is a quite complex project having specific reasons for the way it is setup, but for ease of learning purposes, I've replaced the subject with just names.
For example, I have two tables:
Servers (stores unique server names)
Clients (stores all the clients in the company)
server_id
(matches the unique id from servers
table) What I'm trying to do is list the distinct server names from the first table, along with the number of clients that use each server id. But instead of returning the server "id", I'd like to return the "name". Right now I have this:
SELECT DISTINCT server_id, count(server_name) AS count
FROM clients
GROUP BY server_id
This returns the server id instead of the name. How do I fix that? Complete beginner to SQL and mySQL by the way.
Try
SELECT s.server_id,
s.server_name,
COUNT(*) count_clients
FROM clients c JOIN servers s
ON c.server_id = s.server_id
GROUP BY s.server_id, s.server_name
Output:
| SERVER_ID | SERVER_NAME | COUNT_CLIENTS |
-------------------------------------------
| 1 | server1 | 2 |
| 2 | server2 | 4 |
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.