MySql count() to return 0 if no records found

前端 未结 6 1471
太阳男子
太阳男子 2020-12-17 20:10

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 wa

相关标签:
6条回答
  • 2020-12-17 20:52

    I think you simply need query like this

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

    vb example

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

    then write

    <%=count("totid")%>
    
    0 讨论(0)
  • 2020-12-17 20:54

    Try Like this::

    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
    

    To return 0 instead of null in MySQL
    USE

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

    USE with count() having join of 2 tables

    Tables

    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
    

    Query

    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
    

    Result is

     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 
    
    0 讨论(0)
  • 2020-12-17 20:56

    There is no record for the month of January that is why you are getting no result. One solution that works is by joining a subquery with contains list of months that you want to be shown on the list.

    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
    
    • SQLFiddle Demo

    OUTPUT

    ╔══════════╗
    ║ TOTALREC ║
    ╠══════════╣
    ║        0 ║
    ║        7 ║
    ║        9 ║
    ╚══════════╝
    
    0 讨论(0)
  • 2020-12-17 20:56

    Did you try IFNULL() the right way? Maybe try IFNULL(Count(id), 0) in a SELECT clause with join.

    0 讨论(0)
  • 2020-12-17 20:58

    If the result set had no posts during that time period, you won't get any results to count, hence why it doesn't show.

    You would need to either join to another table that has all of the year months or fill in the data programmatically when the results are returned. I can't think of another way to do this, but perhaps it's possible.

    Also, as others have said, separate the group by a comma.

    0 讨论(0)
  • 2020-12-17 21:02

    COALESCE is what you could use, if you have a table of dates and left joined against it. It goes left to right to return the first non null value. Your group by does look a little nutty at the minute, I have adjusted it.

    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
    

    Where dates table is..

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

    See here : http://easywebapps.blogspot.co.uk/2011/07/mysql-how-to-create-calendar-table.html

    0 讨论(0)
提交回复
热议问题