12

我每个月都有一组帖子。现在我需要一个数组,其中包含每个月发布的帖子的总记录。我在 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 |
+----------+

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

+----------+
| 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 |
+----+---------------------+
4

7 回答 7

18

那个月没有记录,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 ║
╚══════════╝
于 2013-05-19T16:00:35.570 回答
5

你尝试IFNULL()了正确的方法吗?也许尝试IFNULL(Count(id), 0)SELECT带有连接的子句中。

于 2013-05-19T15:47:08.887 回答
5

COALESCE如果您有一个日期表并与之相对,那么您可以使用它。它从左到右返回第一个非空值。你的 group by 现在看起来有点疯狂,我已经调整过了。

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

于 2013-05-19T15:50:07.387 回答
1

如果结果集在该时间段内没有帖子,您将不会得到任何结果来计算,因此它为什么不显示。

您需要连接到另一个包含全年月份的表,或者在返回结果时以编程方式填写数据。我想不出另一种方法来做到这一点,但也许这是可能的。

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

于 2013-05-19T15:54:55.737 回答
1

试试这样::

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
USE中返回 0 而不是 null

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

与具有 2 个表连接的 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 
于 2019-11-01T08:31:29.047 回答
0

您可以尝试简单地使用您的sql。我的旧sql是这样的

select c.*, dynamic_count from channel c 
    left join (select channel_id, count(*) as dynamic_count from dynamic group by channel_id) d
             on c.id = d.channel_id;

如果动态不匹配,我会得到 null。但是如果我像这样更改我的 sql:

select c.*, count(d.id) as dynamic_count from channel c
    left join dynamic d on c.id = d.channel_id
    group by c.id;

如果没有动态匹配,我得到 0;希望它有帮助!

于 2021-11-18T03:49:49.743 回答
0

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

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

例子

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

然后写

<%=count("totid")%>
于 2016-10-02T20:10:27.893 回答