12

I have a set of posts on monthly basis. Now i need an array which contains total records of posts posted in each month. I tried below MySql query, Its working fine, but I was expecting 0(Zero) for months where there is no records. Here its not returning 0.

I read that COUNT() will not return '0', So how do i achieve this?

I tried IFNULL(), and COALESCE() but still getting the same result. Please help with this query. Thank You......

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

Got Result:

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

Expected Result (Where there is no posts for January):

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

Sample Data:

+----+---------------------+
| 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結果が得られません。機能する 1 つの解決策は、リストに表示したい月のリストを含むサブクエリを結合することです。

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 に答える
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で null の代わりに 0 を返すには

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

このようなクエリが必要だと思います

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")%>
于 2016-10-02T20:10:27.893 に答える