0

我有一个表名为 a_ad_display 以下列

displayId | campaignId | displaytime(date)

1          |    2      | 2012-12-19 12:41:08
2          |    2      | 2012-12-19 01:41:08
3          |    1      | 2012-12-20 10:41:08
4          |    1      | 2012-12-20 02:41:08
5          |    1      | 2012-12-21 12:41:08
6          |    2      | 2012-12-21 01:41:08
7          |    1      | 2012-12-21 12:41:08
8          |    1      | 2012-12-21 02:41:08

应该有一个具有相同campaignId的多个条目。现在我想要的是首先获取a_ad_display中计数最高的campaignId,对于那个campaignId,我想获得日期内的计数总数

例如..从上表中,我有 1 个行数最高的活动 ID

所以对于 1 号campaignId 我想显示这样的记录..

        **Date**     | **countPerDay**
 2012-12-19          |      0
 2012-12-20          |      2
 2012-12-21          |      3

我怎样才能在mysql数据库中实现这一点......希望任何人都可以指导我......

4

4 回答 4

3

您可以使用此查询

select 
    count(campaignId) as TCount,
    DATE(displayTime) as `Date`
from a_ad_display
where campaignId = 1
group by DATE(displayTime)

虽然它不会显示 0。

编辑 :

OK 这已修改并获取您的结果。

select
  date(a.displayTime) as `DisplayTime`,
  ifnull(l.TCount,0) as TCount
from a_ad_display as a
  left join (select
           count(campaignId) as TCount,
           displayTime
         from a_ad_display
         where campaignId = 1
         group by DATE(displayTime)) as l
    on date(a.displayTime) = date(l.displayTime)
group by DATE(a.displayTime)

演示

更多编辑:

select
  date(a.displayTime) as `DisplayTime`,
  ifnull(l.TCount,0) as TCount
from a_ad_display as a
  left join (select
           count(campaignId) as TCount,
           displayTime
         from a_ad_display
         where campaignId = 1
         group by DATE(displayTime)) as l
    on date(a.displayTime) = date(l.displayTime)
group by DATE(a.displayTime)
order by a.displaytime desc limit 5

这是您可以选择最新 5 条记录的方法。如果您想选择最新的广告系列,您可以使用子查询而不是 1 查看此查询

select
  date(a.displayTime) as `DisplayTime`,
  ifnull(l.TCount,0) as TCount
from a_ad_display as a
  left join (select
       count(campaignId) as TCount,
       displayTime
     from a_ad_display
     where campaignId = (select  campaignId from a_ad_display group by campaignId order by count(campaignId) desc limit 1)
     group by DATE(displayTime)) as l
    on date(a.displayTime) = date(l.displayTime)
group by DATE(a.displayTime)

编辑:

我已经编辑了查询,这将获取更高发生率的活动 ID 并相应地获取结果

演示

于 2013-02-13T06:20:47.437 回答
2

这也将显示 0

 select 
       sum(case when campaignId = @inputCompainid then 1 else 0 end) as TCount, 
        DATE(displayTime) as `Date`
    from a_ad_display
    group by DATE(displayTime)
于 2013-02-13T06:27:39.217 回答
1

以下查询为您提供了每个活动 ID 的记录列表

select campaignId,date(displaytime) as Date,count(*) as countPerDay from a_ad_display group by campaignId,date(displaytime);

如果您需要特定的campaignId,请使用:

select campaignId,date(displaytime) as Date,count(*) as countPerDay from a_ad_display group by campaignId,date(displaytime) having campaignId=1;
于 2013-02-13T06:22:50.587 回答
1

很抱歉问这个问题,但是这个查询中哪一个更好?

1.raheel shan先生的回答

EXPLAIN SELECT DATE(a.displayTime) AS `DisplayTime`,IFNULL(l.TCount,0) AS TCount
FROM a_ad_display AS a
LEFT JOIN (SELECT
       COUNT(campaignId) AS TCount,
       displayTime
     FROM a_ad_display
     WHERE campaignId = 1
     GROUP BY DATE(displayTime)) AS l
ON DATE(a.displayTime) = DATE(l.displayTime)
GROUP BY DATE(a.displayTime);


id  select_type  table         type  possible_keys  key  key_len  ref  rows  Extra                                            
1   PRIMARY      <derived2>    ALL                                     2                                                   
1   PRIMARY      a             ALL                                     8     Using temporary; Using filesort               
2   DERIVED      a_ad_display  ALL                                     8     Using where; Using temporary; Using filesort  


2.或者使用这个

EXPLAIN SELECT DATE(a_ad_display.`displaytime`) AS `DATE`, 
               COUNT(a.`campaignId`) AS `COUNT Per Day` 
        FROM a_ad_display 
        LEFT JOIN (SELECT `a_ad_display`.`displayId`,
                          `a_ad_display`.`campaignId` 
                   FROM `a_ad_display` 
                   WHERE `a_ad_display`.`campaignId`=1) a 
        ON (`a_ad_display`.`displayId`=a.`displayId`)
        GROUP BY `a_ad_display`.`displaytime`;

id  select_type  table         type  possible_keys  key  key_len  ref  rows  Extra                              
1   PRIMARY      a_ad_display  ALL                                     8     Using temporary; Using filesort  
1   PRIMARY      <derived2>    ALL                                     5                                       
2   DERIVED      a_ad_display  ALL                                     8     Using where                      

感谢之前和之后:D


编辑最高的campaignId出现

SELECT DATE(a_ad_display.`displaytime`) AS `DATE`, COUNT(a.`campaignId`) AS `COUNT Per Day` FROM a_ad_display 
LEFT JOIN (SELECT `a_ad_display`.`displayId`,`a_ad_display`.`campaignId` FROM `a_ad_display`
   INNER  JOIN (SELECT COUNT(`a_ad_display`.`campaignId`) AS `count`, `a_ad_display`.`campaignId` 
        FROM `a_ad_display` 
        GROUP BY `a_ad_display`.`campaignId` 
        ORDER BY `count` DESC 
        LIMIT 1) b ON (a_ad_display.`campaignId`=b.`campaignId`)
   ) a ON (`a_ad_display`.`displayId`=a.`displayId`)
GROUP BY `DATE`;

分组依据a_ad_displaydisplaytime; /* 这个语句有误...,对不起*/ 现在应该可以工作了.....再试一次。你可以在这里检查http://sqlfiddle.com/#!2/bc75b/1,希望它能正常工作:D

于 2013-02-13T07:25:10.357 回答