Jump to content

Recommended Posts

I hope I can explain this correctly. I want to get the number of users for each of the companies in my db. this is how my db is laid out:

 

table companies (cols are comma seperated):

id,name,

 

table clients (cols are comma seperated):

id,name,company_id

 

Now clients table relates to the companies table via the company_id

 

My problem is constructing a query that will tell me the number of clients for each company. What I mean is that I will not have the specific id when running the query. I need this for a report on my site so i can see how many users/clients are from a specific company. I've tried unions, left joins, regular joins and I can't seem to come up with a way to do this with one query.

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/136435-solved-complicated-query-need-help/
Share on other sites

SELECT cl.company_id, COUNT(cl.id) AS clients, co.name FROM clients cl JOIN companies co ON co.company_id = cl.company_id GROUP BY cl.company_id;

 

 

If I understood what you're trying to do correctly.

 

Not very complicated, by the way.  A simple join with a COUNT....

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.