MySql更新表来自另一个加入和分组

时间:2016-12-26 17:27:59

标签: mysql database join

如果我有桌面日志:

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

和另一个具有相同日期& P_id如下:

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

我需要MySQL查询来填充报表中的点击并根据键(日期& P_id):

clicks = 
    count of rows having (Date & P_id) in Report table 
          divided by 
    count of rows having (Date & P_id) and Type is adClick

所以表格将是:

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

示例,第一行:

2016-9-1   11      4 / 1 

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

到目前为止我尝试过:

UPDATE report AS r 

INNER JOIN 
(
    SELECT 
        *, count(id) AS count_value
    FROM 
        logs
    WHERE
        type= "adClick"
    GROUP BY 
        date,p_id

) log

ON  r.date=log.date AND  r.p_id=log.p_id

SET r.clicks=(log.count_value / (SELECT COUNT(lof) from report) );

提前致谢

3 个答案:

答案 0 :(得分:1)

您可以使用条件聚合执行此操作:

UPDATE report AS r INNER JOIN 
       (SELECT date, p_id, count(*) AS cnt,
               SUM(type = 'adClick') as cnt_adclick
        FROM logs
        GROUP BY  date,p_id
       ) l
       ON r.date = l.date AND r.p_id = l.p_id
    SET r.clicks = cnt_adclick / cnt;

您也可以使用avg()代替分部执行此操作:

UPDATE report AS r INNER JOIN 
       (SELECT date, p_id, 
               AVG(type = 'adClick') as avg_clicks
        FROM logs
        GROUP BY  date,p_id
       ) l
       ON r.date = l.date AND r.p_id = l.p_id
    SET r.clicks = avg_clicks;

答案 1 :(得分:1)

尝试使用CONCAT()

UPDATE report AS r
INNER JOIN (
    SELECT
        date,
        p_id,
        count(*) AS count,
        SUM(type = 'adClick') AS count_adclick
    FROM
        logs
    GROUP BY
        date,
        p_id
) log ON r.date = log.date
AND r.p_id = log.p_id
SET r.clicks = CONCAT(log.count_adclick, ' / ', log.count);

答案 2 :(得分:0)

谢谢大家,以下为我完成了这项工作:

UPDATE report AS r 
INNER JOIN 
(
    SELECT cnt_adclick,count(*) as cnt_report,date, report.P_id
    FROM report
    INNER JOIN 
    (
        SELECT date as date, P_id, SUM(event_type = 'adClick') as cnt_adclick
        FROM logs
        GROUP BY date,P_id
    ) inner_log

    ON report.date = inner_log.date AND report.P_id = inner_log.P_id
    GROUP BY report.date, report.P_id
) l

ON r.date = l.date AND r.P_id= l.P_id
SET r.clicks = cnt_adclick / cnt_report;