我需要帮助才能根据每个子渠道
上的买家数量,通过mysql查询获取每个主渠道上的买家数量基本上我有两个表,即 tbl_channel 和 tbl_buyer ,如下所示:
条件:
我想要的结果:
到目前为止,我只能进行查询以显示子频道的买家数量,如下所示:
SELECT count(aa.buyer_id) as Number_of_buyers FROM (SELECT b.buyer_id
FROM tbl_channel a
RIGHT JOIN tbl_buyer b on (a.id=b.channel_id)
WHERE a.parentid_channel_id = 2 /* PARENT ID */
GROUP by b.buyer_id ORDER BY b.channel_id) aa
您可以帮我完成一个完整的查询以产生如上所示的结果吗?谢谢你:)
答案 0 :(得分:3)
SELECT a.channel_id `Channel ID`,
a.channel_title `Channel Title`,
COUNT(DISTINCT c.buyer_id) `Number of Buyers`
FROM tbl_Channel a
INNER JOIN tbl_Channel b
ON a.channel_ID = b.parent_channel_id
INNER JOIN tbl_buyer c
ON b.channel_ID = c.channel_ID
WHERE a.parent_channel_id = 0
GROUP BY a.channel_id,
a.channel_title