我有 4 张 table :
- ad (id, ...)
- website (id, title, URL, ...)
- space (id, website_id, ...)
- ad_space_counter (id, ad_id, website_id, space_id, impression_counter, click_counter)
一个网站可能分配有 2 或 3 个空间。
现在我希望实现的是进行一次选择并获得以下结果:
- ad_id
- website_id
- URL (from website table)
- title (from website table)
- impression_counter (but the TOTAL of all spaces assigned to that specific website_id)
- click_counter (but the TOTAL of all spaces assigned to that specific website_id)
因此,如果我们有 ad_id =1 和 website_id = 1,但有 3 个空格且 website_id = 1,那么 ad_space_counter 将生成 3 个条目,每个条目都有自己的特定展示次数和点击计数器。此 SELECT 必须获取所有这些的总和并将其显示在一个结果中。
谢谢。
最佳答案
请尝试使用以下内容,请注意“ad_id”列未包含在列列表中。因此下面的查询可以被视为网站的统计数据。
select
a.website_id,
website.url,
website.title,
sum(a.impression_counter) as impression_counter,
sum(a.click_counter) as click_counter
from ad_space_counter a,
ad,
website
space
where ad.id = a.ad_id
and website.id = a.website_id
and space.id = a.space_id
group by
a.website_id,
website.url,
website.title
关于php - 包含计数的 4 路 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19907718/