使用计数联合或使用总和连接 - MySQL

Posted

技术标签:

【中文标题】使用计数联合或使用总和连接 - MySQL【英文标题】:Union with Count OR Join with Sum - MySQL 【发布时间】:2010-11-09 02:25:12 【问题描述】:

我想在查询中组合三个表 - 日期、潜在客户和点击。

表格如下所示:

日期:

|date|

领导:

id|time|commission

点击:

id|time|commission

表格日期只是存储日期,用于在没有点击或引导的情况下获取日期。

所以如果我们在表格中有以下数据:

日期:

2009-06-01
2009-06-02
2009-06-03

领导:

1|2009-06-01|400
2|2009-06-01|300
3|2009-06-03|350

点击:

1|2009-06-01|1
2|2009-06-03|2
3|2009-06-03|2
4|2009-06-03|0

我想获取日期,点击次数,点击产生的佣金(有点击不给佣金),潜在客户数量,潜在客户产生的佣金和总佣金。因此,通过上面的表格,我想得到:

2009-06-01|1|1|2|700|701|
2009-06-02|0|0|0|0|0
2009-06-03|3|4|1|350|354|

我尝试过以下联合:

 SELECT  
    campaign_id, 
    commission_date,  
    SUM( click_commission ) AS click_commission,
    click,
    SUM( lead_commission ) AS lead_commission ,  
    lead,
    SUM( total_commission ) as total_commission
    FROM(
        SELECT  
            click.campaign_id AS campaign_id, 
            DATE( click.time ) AS commission_date, 
            click.commission AS click_commission, 
            (SELECT count(click.id) from click GROUP BY date(click.time)) as click,
            0 as lead_commission,
            0 as lead,
            click.commission AS total_commission
        FROM click
        UNION ALL
        SELECT 
            lead.campaign_id AS campaign_id, 
            DATE( lead.time ) AS commission_date, 
            0 as click_commission,
            0 as click,
            lead.commission AS lead_commission, 
            lead.id as lead,
            lead.commission AS total_commission
        FROM lead
        UNION ALL
        SELECT 
            0 AS campaign_id, 
            date.date AS commission_date, 
            0 AS click_commission, 
            0 as click,
            0 AS lead_commission, 
            0 as lead,
            0 AS total_commission
        FROM date 
    ) AS foo 
    WHERE commission_date BETWEEN '2009-06-01' AND '2009-07-25' 
    GROUP BY  commission_date 
    ORDER BY commission_date LIMIT 0, 10

但这不适用于计算点击次数和潜在客户数量,上面的代码给出了所有潜在客户的正确点击量 bot 0。如果我移动代码并从潜在客户表中进行选择,我会在所有点击中获得潜在客户 bot 0。我一直无法找到从查询中获取这两个计数的方法。

所以我尝试了左连接:

SELECT
    date.date as date, 
    count( DISTINCT click.id ) AS clicks, 
    sum(click.commission) AS click_commission, 
    count( lead.id ) AS leads, 
    sum(lead.commission) AS lead_commission
FROM date
LEFT JOIN click ON ( date.date = date( click.time ) )
LEFT JOIN lead ON ( date.date = date( lead.time ) )
GROUP BY date.date
LIMIT 0 , 30 

此查询的问题是,如果在某个日期有多次点击或潜在客户,它将返回预期值 * 2。因此,在 2009 年 6 月 1 日,它将返回 1400 而不是预期的 700 潜在客户佣金。

所以在 UNION 中,我的计数有问题,而在左连接中,SUM 不起作用。

如果可能的话,我真的很想坚持使用 UNION,但我还没有找到一种方法来从中获得两个计数。

(这是对this 早先问题的跟进,但由于我没有要求计数,因此我发布了一个新问题。)

【问题讨论】:

如果有人知道 UNION ALL 的解决方案,我会非常想知道这个解决方案,因为 UNION 比 LEFT JOIN 更容易使用。 【参考方案1】:
SELECT  date,
        COALESCE(lcomm, 0), COALESCE(lcnt, 0),
        COALESCE(ccomm, 0), COALESCE(ccnt, 0),
        COALESCE(ccomm, 0) + COALESCE(lcomm, 0),
        COALESCE(ccnt, 0) + COALESCE(lcnt, 0)
LEFT JOIN
        (
        SELECT  date, SUM(commission) AS lcomm, COUNT(*) AS lcnt
        FROM    leads
        GROUP BY
                date
        ) l
ON      l.date = d.date
LEFT JOIN
        (
        SELECT  date, SUM(commission) AS ccomm, COUNT(*) AS ccnt
        FROM    clicks
        GROUP BY
                date
        ) с
ON      c.date = d.date
FROM    date d

【讨论】:

不得不做一些小改动,但终于成功了,谢谢!【参考方案2】:

我使用的代码是根据 Quassnoi 的建议构建的:

SELECT  date,
        COALESCE(ccomm, 0) AS click_commission, COALESCE(ccnt, 0) AS click_count,
        COALESCE(lcomm, 0) AS lead_commision, COALESCE(lcnt, 0) AS lead_count,
        COALESCE(ccomm, 0) + COALESCE(lcomm, 0) as total_commission
FROM    date d
LEFT JOIN
        (
        SELECT  DATE(time) AS lead_date, SUM(commission) AS lcomm, COUNT(*) AS lcnt
        FROM    lead
        GROUP BY
                lead_date
        ) l
ON     lead_date = date
LEFT JOIN
        (
        SELECT  DATE(time) AS click_date, SUM(commission) AS ccomm, COUNT(*) AS ccnt
        FROM    click
        GROUP BY
                click_date
        ) с
ON      click_date =  date

【讨论】:

以上是关于使用计数联合或使用总和连接 - MySQL的主要内容,如果未能解决你的问题,请参考以下文章

SQL:联合或自加入

在 knexjs mysql 中取联合多列后总和不起作用

MySQL数据库联合查询与连接查询

mysql 联合查询

mysql联合索引有大于小于能用到索引吗

Mysql联合查询语句语法