查询1:
SELECT count(id) as conversions, SUM(user_payout) as amount, geoip.country_name, geoip.country_code
FROM conversions
LEFT JOIN geoip ON conversions.end_ip BETWEEN geoip.start_long AND geoip.end_long
WHERE user_id = 1
AND type != ''
AND status = 1
AND month(created_at) = month(now())
GROUP BY country_name
ORDER BY country_name ASC
结果:
查询2:
SELECT count(id) as clicks, geoip.country_name, geoip.country_code
FROM clicks
LEFT JOIN geoip on clicks.ip BETWEEN geoip.start_long AND geoip.end_long
WHERE user_id = 1
AND month(created_at) = month(now())
GROUP BY country_name
ORDER BY country_name ASC
结果:
我想将这两者合并为每个国家/地区,它会显示点击次数,转化次数,country_name和country_code。
无法弄清楚如何做到这一点。
谢谢!
答案 0 :(得分:2)
您可以使用子查询和left outer join
:
select clicks.country_code, clicks.country_name,
coalesce(clicks.clicks, 0) as clicks,
coalesce(conversions.conversions, 0) as conversions
from (<subquery 2>) clicks left outer join
(<subquery 1>) conversions
on clicks.country_code = conversions.country_code;
这假设所有国家/地区至少有一次点击。
编辑:
如果国家/地区列表位于geo_ip
,您可以执行以下操作:
select gi.country_code, gi.country_name, clicks.clicks, conversions.conversions
from (select distinct country_code, country_name
from geo_ip
) gi left outer join
(<subquery 2>) clicks
on clicks.country_code = gi.country_code left outer join
(<subquery 1>) conversions
on conversions.country_code = gi.country_code;
答案 1 :(得分:0)
左连接点击第一个查询以进行geoip。
或者您可以使用联盟但是您只需在select语句中选择那些结果,就可以将每个结果仅显示点击次数,转化次数,country_name和country_code。