我有三个表:域名,注册和跟踪。
我目前有一个所有域名列表和使用此查询的访问者人数:
SELECT sites.id, sites.domain, count(tracking.domain) as tracked FROM sites
LEFT JOIN tracking ON sites.domain = tracking.domain
GROUP BY sites.domain
ORDER BY sites.id;
现在,我还希望从名为signups的新表中获取计数(每个域)。
我已经达到了这个目标:
SELECT sites.id, sites.domain, count(tracking.domain) as tracked, signers.cc FROM sites
LEFT JOIN tracking ON sites.domain = tracking.domain
LEFT JOIN (
SELECT signups.domain, count(*) as cc FROM signups
GROUP BY signups.domain
) as signers
USING (domain)
ORDER BY sites.id;
但这似乎并不完全正确。有什么建议吗?
答案 0 :(得分:0)
在这里:(你可以随意添加外部联接,我只是指向正确的方向)
with trackingCount as
(
select sites.domain,
count(1) as tracked
from sites,
tracking
where tracking.domain = sites.domain
group by sites.domain),
signupsCount as
(
select sites.domain,
count(1) as signedUP
from sites,
signups
where signups.domain = sites.domain
group by sites.domain)
select *
from trackingCount tc,
signupsCount sc
where tc.domain = sc.domain;
给出:
domain tracked signedUP
A 4 2
B 1 4
你可以在这里玩弄它: