MySQL LEFT JOIN与GROUP BY和WHERE IN(子查询) [英] MySQL LEFT JOIN with GROUP BY and WHERE IN (sub query)

查看:154
本文介绍了MySQL LEFT JOIN与GROUP BY和WHERE IN(子查询)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表,每个日期都有一些统计信息,我想在MySQL中列出.对于某些日期,将没有统计信息,因此结果应类似于以下内容:
2013-03-01:3
2013-03-02:2
2013-03-03:0
2013-03-04:1

I have one table with some statistics per date, which I want listed out with MySQL. For some dates there will be no statistics, so the result should look something like this:
2013-03-01: 3
2013-03-02: 2
2013-03-03: 0
2013-03-04: 1

我发现,可以使用单独的表格(包含所有可能的日期和LEFT JOIN)来解决0-零-的问题.到目前为止一切顺利.

I figured out that filling in the gaps with 0 -zero- could be solved with a separate table with all possible dates and LEFT JOIN. So far so good.

统计信息(展示次数)位于"campaigndata"表中:

The statistics (impressions) is in the table 'campaigndata':

id - int(11)
date - date
campaignid - int(11)
impressions - int(11)

但是我只想获取一些统计信息.更具体地说,我只希望来自"campaigndata"行中的"campaignid"位于表"campaignfilter"中且"campaigntype"设置为1的行(例如).

But I want to get only some of the statistics. To be more specific, I only want the rows from 'campaigndata' where 'campaignid' is in the table 'campaignfilter' with 'campaigntype' set to 1 (as an example).

这是表"campaignfilter":

This is the table 'campaignfilter':

id - int(11)
campaigntype - int(11)
campaignid - int(11)

有人知道如何做到这一点吗?

Anyone have clue how this could be done?

PS:"campaigndata"表的结构已被锁定,因为它是基于从外部系统自动导入的.

PS: The structure of table 'campaigndata' is pretty much locked, since it is based on an automatic import from an external system.

样品记录

CREATE TABLE demo_campaigndata (
  id int(11) NOT NULL AUTO_INCREMENT,
  date date NOT NULL,
  campaignid int(11) NOT NULL,
  impressions int(11) NOT NULL,
  PRIMARY KEY (id)
);
INSERT INTO demo_campaigndata (id, date, campaignid, impressions) VALUES
(1, '2013-03-03', 1, 100),
(2, '2013-03-03', 2, 100),
(3, '2013-03-03', 3, 100),
(4, '2013-03-04', 2, 100),
(5, '2013-03-05', 1, 100),
(6, '2013-03-05', 2, 100);


CREATE TABLE demo_campaignfilter (
  id int(11) NOT NULL AUTO_INCREMENT,
  campaigntype int(11) NOT NULL,
  campaignid int(11) NOT NULL,
  PRIMARY KEY (id)
);
INSERT INTO demo_campaignfilter (id, campaigntype, campaignid) VALUES
(1, 1, 1),
(2, 1, 3);


CREATE TABLE demo_calendar (
  date date NOT NULL,
  PRIMARY KEY (date)
);
INSERT INTO demo_calendar (date) VALUES
('2013-03-01'),
('2013-03-02'),
('2013-03-03'),
('2013-03-04'),
('2013-03-05');

期望结果

2013-03-01: 0
2013-03-02: 0
2013-03-03: 200
2013-03-04: 0
2013-03-05: 100

推荐答案

SELECT  a.date, COUNT(b.campaignid) totalStat
FROM    campaigndata a
        LEFT JOIN campaignfilter b
            ON  a.campaignid = b.campaignid AND
                b.campaigntype = 1
GROUP   BY a.date

要进一步获得有关联接的知识,请访问下面的链接:

To further gain more knowledge about joins, kindly visit the link below:

更新1

SELECT  a.date, 
        COALESCE(b.totals,0) totals
FROM    demo_calendar a
        LEFT JOIN
        (
            SELECT  a.date, SUM(impressions) totals
            FROM    demo_campaigndata a
                    INNER JOIN demo_campaignfilter b
                        ON a.campaignid = b.campaignid
            WHERE   b.campaigntype = 1
            GROUP   BY a.date
        ) b ON a.date = b.date

  • SQLFiddle演示
  • 这篇关于MySQL LEFT JOIN与GROUP BY和WHERE IN(子查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆