我的查询有问题。我有两个表,一个有点击,另一个有转化,如下所示
表:转化
id | ref id | registered | tempo | prod_cod
----------------------------
1 | 1 | 04/05/2018 | 15385950 | oggn-1
2 | 1 | 05/05/2018 | 15385950 | oggn-1
3 | 1 | 06/05/2018 | 15385950 | oggn-1
表:点击次数
id | ref id | registered | tempo | prod_cod | ip | user_agent
----------------------------
1 | 1 | 05/05/2018 | 15385950 | oggn-1 | 192.168.1 | Mozilla....
2 | 1 | 06/05/2018 | 15385950 | oggn-1 | 192.168.1 | Mozilla....
3 | 1 | 07/05/2018 | 15385950 | oggn-1 | 192.168.1 | Mozilla....
我希望过去七天的ref id选择的结果是:
表:结果
registered | clicks | conversion
----------------------------
04/05/2018 | 0 | 1
05/05/2018 | 1 | 1
06/05/2018 | 1 | 1
07/05/2018 | 1 | 0
这是我的查询,但效果不佳,因为如果两个数据库中都没有,则注册的值不会给出所有结果。
SELECT tbl_affiliate_traffic.registered,
Count(DISTINCT tbl_affiliate_traffic.id) AS tarffic,
Count(DISTINCT tbl_affiliate_conversion.id) AS conversion
FROM tbl_affiliate_traffic CROSS
JOIN tbl_affiliate_conversion
ON tbl_affiliate_conversion.tempo >= '1524891602'
AND tbl_affiliate_conversion.registered =
tbl_affiliate_traffic.registered
AND tbl_affiliate_conversion.ref_id = '1'
WHERE tbl_affiliate_traffic.tempo >= '1524891602'
AND tbl_affiliate_traffic.ref_id = '1'
GROUP BY registered
答案 0 :(得分:0)
如果可能出现问题,您无法在单个查询中减慢此问题,在较高数据量中需要花费太多时间。对这种场景和数组重新转换使用两个查询。
查询1:
SELECT COUNT(*) AS _count,registered FROM `Conversion` GROUP BY registered
执行此查询后,您需要像这样填充数组
$ Conversion = ['04 / 05/2018'=> 1,'05 / 05/2018'=> 1]
查询2:
SELECT COUNT(*) AS _count,registered FROM `Clicks` GROUP BY registered
并像这样填充数组“点击”
$ Clicks = ['04 / 05/2018'=> 0,'05 / 05/2018'=> 1]
之后,你可以合并这个两个数组
foreach($Conversion as $key=>$val){$result[$key]['Conversion'] = $val}
foreach($Clicks as $key=>$val){$result[$key]['Clicks'] = $val}
最后,你可以得到结果。