Shounak Gupte
Shounak Gupte

Reputation: 69

Query regarding MYSQL Join & Count

I have 2 tables:

clients(client_id, client_name)
client_posts(client_id, website_id, category, posted_by)

how do i return the following data:

CLIENT_ID    CLIENT_NAME            NUM
    1       test client 1           30
    2       test client 2           17
    3       test client 3           8

where NUM is the number(count) of times the client ID is found in table client_posts

Upvotes: 2

Views: 62

Answers (2)

Suhel Meman
Suhel Meman

Reputation: 3852

Query :

select cl.client_id,cl.client_name,count(cp.client_id) as NUM
 from clients cl 
 left join client_posts cp on (cl.client_id=cp.client_id)
 group by cp.client_id;

Fiddle Example

Upvotes: 0

NewInTheBusiness
NewInTheBusiness

Reputation: 1475

SELECT client_id, client_name, count(*) AS NUM
FROM client_posts cp
LEFT JOIN clients c ON c.client_id = cp.client_id
GROUP BY client_id

Upvotes: 2

Related Questions