MySQL计数包括0

时间:2013-11-26 03:58:53

标签: mysql

我试图在几个月的时间内按月计算总数。这是我正在使用的查询:

SELECT COUNT(siteCount) AS Hits, DATE_FORMAT(TIMESTAMP,'%b') AS Month 
FROM count 
WHERE redirectDomain LIKE 'http://domain.com' 
AND DATE_FORMAT(TIMESTAMP,'%Y') = '2013'
AND DATE_FORMAT(TIMESTAMP,'%b') BETWEEN 'Sep' AND 'Nov'  
GROUP BY DATE_FORMAT(TIMESTAMP,'%b')

这非常有效,除非计数为0. 11月有0个条目,因此不会显示在结果中。如何将Nov包含在结果中并显示0?

我猜我需要ISNULL或CASE,但我已经尝试了两种,并且无法弄清楚如何使这项工作。

感谢您的帮助。

5 个答案:

答案 0 :(得分:0)

你不能仅仅count表。以下查询可能对您有帮助。

已更新

SELECT month_tab.Mo, IF(x.Hists IS NULL, 0, x.Hists)
FROM
(
    SELECT 'Jan' AS Mo
    UNION
    SELECT 'Feb' AS Mo
    UNION 
    SELECT 'Jun' AS Mo
    UNION
    SELECT 'Aug' AS Mo
    UNION
    SELECT 'Sep' AS Mo
    UNION
    SELECT 'Oct' AS Mo
    UNION
    SELECT 'Nov' AS Mo
) month_tab
LEFT JOIN 
(
SELECT SUM(count.siteCount) AS Hists, DATE_FORMAT(timestamp, '%b') Mo
FROM count
WHERE redirectDomain LIKE 'http://domain.com'
GROUP BY Mo
) x ON month_tab.Mo = x.Mo;

答案 1 :(得分:0)

案例肯定是一种选择......

SELECT 
     CASE WHEN 
              COUNT(`siteCount`) = 0 
     THEN 
          '0' 
     ELSE 
          COUNT(`siteCount`) 
          END AS Hits, 
          DATE_FORMAT(TIMESTAMP,'%b') AS Month

...但在这种情况下不是必需的(请参阅以下注释的完整查询):

SELECT
  COUNT(`siteCount`) AS Hits,

-- See below for an explanation about naming conventions... 'TIMESTAMP' is probably
-- a name you'd want to avoid as well. If you can't avoid it though, use `backticks`.
  DATE_FORMAT(`TIMESTAMP`,'%b') AS Month

-- I recommend changing the name of your database to something other than 'count'
-- because that can really confuse the system and make life much harder on you.
-- It's kind of like naming your daughter 'Daughter' or creating a PHP function
-- named 'function'. In either case you're eventually going to end up with a mess.
-- Fortunately, MySQL allows you to use `backticks` which, in this case, are
-- absolutely crucial (if for no other reason than maintaining sanity). ;)
FROM `count`

WHERE `redirectDomain` LIKE 'http://domain.com'

-- This will return a 4-digit numeric year (removing 'quotes')
AND DATE_FORMAT(`TIMESTAMP`,'%Y') = 2013

-- AND DATE_FORMAT(TIMESTAMP,'%b') BETWEEN 'Sep' AND 'Nov'
-- This doesn't know that you're talking about months... All it knows is to look
-- for values between the group of letters "Sep" and the group of letters "Nov".
-- For example, 'Feb' would not be counted as being between 'Jan' and 'Mar', because
-- it starts with the letter F, while 'Jul' and 'Jun' would.
-- Instead, convert to numbers:
AND DATE_FORMAT(`TIMESTAMP`,'%c') BETWEEN 9 AND 11  

GROUP BY DATE_FORMAT(`TIMESTAMP`,'%b')

-- If you want the results ordered chronologically (instead of alphabetically) you
-- can use the same logic as is written above, converting letters to numbers.
-- However THIS time, use '%m' instead of '%c' because it will add a '0' to the
-- left of the month number. (Delete the next line if that's not what you want).
ORDER BY DATE_FORMAT(`TIMESTAMP`,'%m') ASC

答案 2 :(得分:0)

您是正确的,它没有返回您的month字段,因为没有任何行(这就是您的count为零的原因) - 您可以将count替换为SELECT (SELECT COUNT(c2.siteCount) FROM count c2 WHERE redirectDomain LIKE 'http://domain.com' AND c2.TIMESTAMP = c1.TIMESTAMP) as Hits, DATE_FORMAT(TIMESTAMP, '%b') AS Month, FROM count c1 -- ... subselect将使他们与分组等分开:

{{1}}

我有一种感觉你不应该使用TIMESTAMP ......保留字......?

答案 3 :(得分:0)

我已经解决了你的问题,只需按照这个 -

我创建了一个表..

        create table test(month text,site_count int null);

         insert into test values('Sept',1),('Oct',1);

         insert into test(month) values('Nov');

我没有在'Nov'的site_count列中给出任何值,即'Nov'的site_count为null。现在我尝试根据月份计算site_count列,并尝试获取'0'表示本月的'Nov'月份在site_count列中为空。

我尝试了以下查询并且它有效,即。我在你需要的'11月'的site_count列中得到'0'。

     select month,count(site_count) from test ;

答案 4 :(得分:0)

SELECT SUM(Hits) AS Hits,Month,MonthOrder
FROM
 (
 SELECT COUNT(siteCount) AS Hits, 
        DATE_FORMAT(TIMESTAMP,'%b') AS Month,
        DATE_FORMAT(TIMESTAMP,'%m') AS MonthOrder
 FROM count 
 WHERE redirectDomain LIKE 'http://domain.com' 
    AND DATE_FORMAT(TIMESTAMP,'%Y') = '2013'
    AND DATE_FORMAT(TIMESTAMP,'%b') IN ('Sep','Oct','Nov')
 GROUP BY DATE_FORMAT(TIMESTAMP,'%b')
 UNION SELECT 0 AS Hits, 'Jan' AS Month,'01' AS MonthOrder
 UNION SELECT 0 AS Hits, 'Feb' AS Month,'02' AS MonthOrder
 UNION SELECT 0 AS Hits, 'Mar' AS Month,'03' AS MonthOrder
 UNION SELECT 0 AS Hits, 'Apr' AS Month,'04' AS MonthOrder
 UNION SELECT 0 AS Hits, 'May' AS Month,'05' AS MonthOrder
 UNION SELECT 0 AS Hits, 'Jun' AS Month,'06' AS MonthOrder
 UNION SELECT 0 AS Hits, 'Jul' AS Month,'07' AS MonthOrder
 UNION SELECT 0 AS Hits, 'Aug' AS Month,'08' AS MonthOrder
 UNION SELECT 0 AS Hits, 'Sep' AS Month,'09' AS MonthOrder
 UNION SELECT 0 AS Hits, 'Oct' AS Month,'10' AS MonthOrder
 UNION SELECT 0 AS Hits, 'Nov' AS Month,'11' AS MonthOrder
 UNION SELECT 0 AS Hits, 'Dec' AS Month,'12' AS MonthOrder
 )T1
GROUP BY Month,MonthOrder
ORDER BY MonthOrder ASC

根据您的需要删除您的检查条件,我认为您不再需要IN ('Sep','Oct','Nov')了。 这是sqlFiddle example