3

查询一:

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

结果:
查询结果

我想将两者结合起来,以便为每个国家/地区显示点击次数、转化次数、国家名称和国家代码。

无法弄清楚如何做到这一点。

谢谢!

4

2 回答 2

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;
于 2013-08-29T21:10:22.303 回答
0

Left Join 点击第一个查询到 geoip。

或者您可以使用联合,但您只能通过仅在 select 语句中选择这些结果来获取每个结果以仅显示点击次数、转化次数、国家名称和国家代码。

于 2013-08-29T21:09:00.950 回答