我有两张桌子:
clients(client_id, client_name)
client_posts(client_id, website_id, category, posted_by)
如何返回以下数据:
CLIENT_ID CLIENT_NAME NUM
1 test client 1 30
2 test client 2 17
3 test client 3 8
其中NUM
是在表client_posts中找到客户端ID的次数(次数)
答案 0 :(得分:2)
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
答案 1 :(得分:0)
查询:
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;