mysql查询按日期获取行数据的最大数量?

时间:2013-02-13 06:10:40

标签: mysql sql database inner-join

我有一个表名为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个号码campaignId,行数最多

所以对于1号广告系列我想要显示这样的记录..

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

我怎样才能在mysql数据库中实现这一点...希望有人能指导我......

4 个答案:

答案 0 :(得分:3)

您可以使用此查询

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

虽然不会显示0。

编辑:

确定已修改此内容并获取结果。

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)

Demo

更多编辑:

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)

编辑:

我已经编辑了查询,这将获取更高的出现率campaignId并相应地获取结果

Demo

答案 1 :(得分: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)

答案 2 :(得分:1)

以下查询为您提供每个campaignId的记录列表

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

如果您需要specfic campaignId,请使用以下命令:

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

答案 3 :(得分:1)

sory的问题,但这个查询哪个更好?

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                      

感谢之前&amp;之后: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`;

GROUP BY a_ad_displaydisplaytime; / *在此声明中错误...,抱歉* / 它现在应该工作.....再试一次。 你可以在这里办理登机手续http://sqlfiddle.com/#!2/bc75b/1,希望它可以工作:D