2017-02-03 96 views
0

如果我有表日志:基于结果从另一个查询的MySQL更新表

ID Date  P_id  TYPE 
------------------------------- 
1 2016-9-1 11 adClick  
2 2016-9-1 22 adComplete 
3 2016-9-1 11 adClick 
4 2016-9-3 22 adClick   
5 2016-9-3 22 adClick   
6 2016-9-1 11 adClick   
7 2016-9-3 22 adComplete   
8 2016-9-1 11 adClick   
9 2016-9-3 11 adClick   
------------------------------- 

,并具有相同日期&的p_id另一个表报告如下:

ID Date  P_id  clicks 
-------------------------------- 
1 2016-9-1 11  
2 2016-9-3 11  
3 2016-9-1 11   
4 2016-9-3 11  
5 2016-9-1 22  
6 2016-9-1 11   
5 2016-9-1 22   
--------------------------------- 

我需要MySQL查询根据密钥(日期& P_id)在报表中点击点击数,如果有余数,则随机添加一个直到完成剩余部分,并选择相同的键:

clicks = 
     count of rows having (Date & P_id) in logs table 
     ------------------- Divistion (/) ------------------- 
    count of rows having (Date & P_id) in Report and Type is adClick 

通过键(日期& P_ID)和事件类型各组的计数adClick在日志表是:

2016-9-1 11 --- count--> 4 
2016-9-1 22 --- count--> 0 

2016-9-3 11 --- count--> 1 
2016-9-3 22 --- count--> 2 

而在报告表数:

2016-9-1 11 --- count--> 3 
2016-9-1 22 --- count--> 2 

2016-9-3 11 --- count--> 2 
2016-9-3 22 --- count--> 0 

所以表会成为:

ID Date  P_id  clicks 
-------------------------------- 
1 2016-9-1 11  4/3 = 1 and 1 as remainder 
2 2016-9-3 11  1/2 = 0 and 1 as remainder 
3 2016-9-1 11  4/3 = 1 and 1 as remainder 
4 2016-9-3 11  1/2 = 0 and 1 as remainder 
5 2016-9-1 22  0/2 = 0 
6 2016-9-1 11  4/3 = 1 and 1 as remainder 
5 2016-9-1 22  0/2 = 0 
--------------------------------- 

样本解释更多第一行:

2016-9-1 11  4/3 

4 rows (2016-9-1 11) in logs table with type=adClick by 
3 row (2016-9-1 11) in report table 

我已经做了保存,而不其余的点击的一部分,所以在报告表我的查询记录:

ID Date  P_id  clicks 
    -------------------------------- 
    1 2016-9-1 11   1 
    2 2016-9-3 11   0 
    3 2016-9-1 11   1 
    4 2016-9-3 11   0 
    5 2016-9-1 22   0 
    6 2016-9-1 11   1 
    5 2016-9-1 22   0 
    --------------------------------- 

现在我想传播余 - 也许随机 - 到报告表,以便同一个键的总和保持不变:

ID Date  P_id  clicks 
    -------------------------------- 
    1 2016-9-1 11   1 + 1 
    2 2016-9-3 11   0 + 1 
    3 2016-9-1 11   1 
    4 2016-9-3 11   0 
    5 2016-9-1 22   0 
    6 2016-9-1 11   1 
    5 2016-9-1 22   0 
    --------------------------------- 

所以点击了(2016年9月1日11)的总和,现在是4两个表。

这里是我用用剩余部分之前更新查询:

UPDATE report AS r 
    INNER JOIN 
    (
     SELECT DATE(ctr_date) as report_date, report.placement_id, count(*) as cnt_report, cnt_event_type 
     FROM report 
     INNER JOIN 
     (
      SELECT DATE(ctr_date) as event_date, placement_id, SUM(event_type = 'adClick') as cnt_event_type 
      FROM logs 
      GROUP BY DATE(ctr_date),placement_id 
     ) inner_report 

     ON report.date = inner_report.event_date AND report.placement_id = inner_report.placement_id 
     GROUP BY report.date, report.placement_id 
    ) result_table 

    ON r.date = result_table.report_date AND r.placement_id= result_table.placement_id 
    SET r.clicks = COALESCE((cnt_event_type - MOD(cnt_event_type,cnt_report))/cnt_report ,0); 

我想使用相同的查询,但有限制(余数)为相同的密钥和使用:

SET r.clicks = r.clicks + 1 
+0

在我看来,它可能更容易和更易于维护,要么在代码中执行此操作,要么添加存储的存储过程来执行此操作。哪种方式最好取决于您的使用情况。 – user2120275

回答

0

我保存了临时表中相同键的余数。并且使用该表通过相同键的报告表,并根据临时表中的剩余值增加以前的值。

相关问题