我有一个MYSQL查询问题,我想请一些帮助。 我有2张桌子。
subscriber: id, campaign_id
route: id, service_code, shortcode
我们这样说:campaign_id = shortcode。
我想执行一个查询,该查询接收所有路由,并为每个路由计算订阅者。
我希望输出如下:
Route | COUNT
44123 | 12
44566 | 48
44213 | 2
etc
你能帮帮我吗?
答案 0 :(得分:1)
JOIN
这两个表格,然后使用COUNT
和GROUP BY
。类似的东西:
SELECT
service_code, COUNT(COALESCE(s.id, 0)) AS `Count`
FROM route AS r
LEFT JOIN Subscriber AS s ON r.shortcode = s.compaign_id
GROUP BY service_code
答案 1 :(得分:0)
使用此:
select r.service_code, count(*)
from route r join subscriber s on s.campaign_id = r.shortcode
group by r.service_code;