如果没有找到记录,MySql count()返回0

时间:2013-05-19 15:43:37

标签: mysql sql

我每月都有一组帖子。现在我需要一个数组,其中包含每个月发布的帖子的总记录。我在MySql下面尝试查询,它的工作正常,但是在没有记录的几个月里,我期待0(零)。这里没有返回0。

我读到COUNT()不会返回'0',那么我该如何实现呢?

我尝试了IFNULL()和COALESCE(),但仍然得到了相同的结果。请帮助解决此问题。谢谢......

SELECT
count(id) as totalRec
FROM ('post')
WHERE year(date) =  '2013'
AND monthname(date) IN ('January', 'February', 'March') 
GROUP BY year(date)-month(date)
ORDER BY 'date' ASC

得到结果:

+----------+
| totalRec |
+----------+
|        7 |
|        9 |
+----------+

预期结果(1月份没有帖子):

+----------+
| totalRec |
+----------+
|        0 |
|        7 |
|        9 |
+----------+

示例数据:

+----+---------------------+
| id | date                |
+----+---------------------+
| 24 | 2012-12-16 16:29:56 |
|  1 | 2013-02-25 14:57:09 |
|  2 | 2013-02-25 14:59:37 |
|  4 | 2013-02-25 15:12:44 |
|  5 | 2013-02-25 15:14:18 |
|  7 | 2013-02-26 11:31:31 |
|  8 | 2013-02-26 11:31:59 |
| 10 | 2013-02-26 11:34:47 |
| 14 | 2013-03-04 04:39:02 |
| 15 | 2013-03-04 05:44:44 |
| 16 | 2013-03-04 05:48:29 |
| 19 | 2013-03-07 15:22:34 |
| 20 | 2013-03-15 12:24:43 |
| 21 | 2013-03-16 16:27:43 |
| 22 | 2013-03-16 16:29:28 |
| 23 | 2013-03-16 16:29:56 |
| 11 | 2013-03-17 11:35:12 |
+----+---------------------+

6 个答案:

答案 0 :(得分:15)

January月没有记录,这就是为什么你没有得到任何结果。一个有效的解决方案是加入子查询,其中包含您希望在列表中显示的月份列表。

SELECT count(b.id) as totalRec
FROM   (
            SELECT 'January' mnth
            UNION ALL
            SELECT 'February' mnth
            UNION ALL
            SELECT 'March' mnth
        ) a
        LEFT JOIN post b
            ON a.mnth = DATE_FORMAT(b.date, '%M') AND
               year(b.date) =  '2013' AND 
               DATE_FORMAT(b.date, '%M') IN ('January', 'February', 'March') 
GROUP  BY year(b.date)-month(b.date) 
ORDER  BY b.date ASC

输出

╔══════════╗
║ TOTALREC ║
╠══════════╣
║        0 ║
║        7 ║
║        9 ║
╚══════════╝

答案 1 :(得分:3)

您是否以正确的方式尝试IFNULL()?也许在加入IFNULL(Count(id), 0)子句中尝试SELECT

答案 2 :(得分:3)

COALESCE是您可以使用的,如果您有一个日期表并且左对齐它。它从左到右返回第一个非null值。 你的小组在一分钟看起来有点疯狂,我调整了它。

SELECT
COALESCE(count(id),0) as totalRec
FROM ('post')
LEFT JOIN dates
ON dates.date = post.date
WHERE year(date) =  '2013'
AND monthname(date) IN ('January', 'February', 'March') 
GROUP BY month(date), year(date)
ORDER BY 'date' ASC

日期表是..

DATE
2013-01-01 
2013-01-02
2013-01-03
etc....

见这里:http://easywebapps.blogspot.co.uk/2011/07/mysql-how-to-create-calendar-table.html

答案 3 :(得分:1)

如果结果集在该时间段内没有帖子,则您无法计算任何结果,因此无法显示结果。

您需要加入另一个具有所有年份月份的表格,或者在返回结果时以编程方式填写数据。我无法想到另一种方法,但也许这是可能的。

此外,正如其他人所说,用逗号分隔该小组。

答案 4 :(得分:0)

我认为你只需要这样的查询

SELECT COALESCE(COUNT(id),0) AS totid FROM table

vb示例

Set count=Con.Execute("SELECT COALESCE(COUNT(id),0) AS totid FROM table")

然后写

<%=count("totid")%>

答案 5 :(得分:0)

尝试这样::

SELECT
  count(id) as totalRec
  IF(id NULL, 0, id) As IdList
FROM ('post')
 WHERE year(date) =  '2013'
  AND monthname(date) IN ('January', 'February', 'March') 
  GROUP BY year(date)-month(date)
ORDER BY 'date' ASC

在MySQL中返回0而不是null
使用

  

SELECT id,IF(age IS NULL,0,age)from tblUser

与带有两个表的连接的count()一起使用

  

表格

tblA

tblA_Id    Name     
-----------------
    1       HSP     
    2       MANI     
    3       MEET     
    4       user    
    5       jaani   

tblB

tblB_Id    SongName  tblA_Id
 -----------------------------
    1        song1     3
    2        song2     3
    3        song3     1
    4        song4     1
    5        song4     5

查询

SELECT 
    tblA.tblA_Id,
    tblA.Name,
    tblC.SongCount,
    IF(tblC.SongCount IS NULL, 0, tblC.SongCount) As noOfSong
  FROM  tblA
    LEFT JOIN
    (   
        SELECT 
            ArtistId,count(*) AS SongCount 
        FROM 
            tblB  
        GROUP BY 
            ArtistId
    ) AS tblC
    ON 
        tblA.tblA_Id = NoOfSong.ArtistId

结果是

 tblA_Id    Name     SongCount   noOfSong
 -------------------------------------------
    5       jaani    1           1
    4       user     NULL        0
    3       MEET     2           2
    2       MANI     NULL        0
    1       HSP      2           2 
相关问题