How to return 0 instead of null when using COUNT in MySQL

后端 未结 5 413

I am using this query to return return a list of songs stored in $sTable along with a COUNT of their total projects which are stored in $sTable2.

 /*
     * SQL          


        
相关标签:
5条回答
  • 2021-01-27 07:35

    Use the COALESCE() function. COALESCE() takes at least 2 arguments, calculated in order, and returns the first non-null argument. So COALESCE(null, 0) would return 0, and COALESCE(null, null, null, null, 1) would return 1. Here's MySQL's documentation about COALESCE().

    In re-reading your query, you should be able to get the results you want like this:

    SELECT <all the fields you want>, b.songsID, COUNT(*) AS projects_count
    FROM $sTable b
    LEFT OUTER JOIN $sTable2 bb ON bb.songs_id = b.songsID
    $sWhere
    GROUP BY b.songsID
    $sOrder
    $sLimit
    

    Like I said, this should work, but something about it doesn't feel quite right.

    0 讨论(0)
  • 2021-01-27 07:39

    You don't have to do the join with a subquery. The following should work just fine without the COALESCE etc:

    SELECT ".str_replace(" , ", " ", implode(", ", $aColumns)).", 
    SUM(b.songsID is not null) as countprojects
    FROM $sTable b 
    LEFT JOIN $sTable2 a ON a.songs_id=b.songsID
    GROUP BY ".str_replace(" , ", " ", implode(", ", $aColumns))."
    

    This will return what you ask for in countprojects.

    The way it works: The LEFT JOIN just makes certain you get all data. You can't use COUNT because that would return 1 for the NULL rows. But, if you just use the fact that a boolean TRUE evaluates to 1, and a boolean FALSE evaluates to 0, you can SUM over those results.

    0 讨论(0)
  • 2021-01-27 07:43
    SELECT blahblahblah, IFNULL(bb.projects_count, 0)
    FROM $sTable b
    etc...
    
    0 讨论(0)
  • 2021-01-27 07:45

    COALESCE() returns the first non-null argument. So if you say COALESCE(count(...),0) it will return the count(...) if it's not null, or it will return 0 if the count(...) is null

    0 讨论(0)
  • 2021-01-27 07:47

    Simply add this line in your code after SELECT

    IF(projects_count IS NULL, 0, projects_count) As projects_countList

    Like This:

    $sQuery = "
        SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns)).",
         IF(projects_countIS NULL, 0, projects_count) As projects_countList
    
    FROM $sTable b 
    LEFT JOIN (SELECT COUNT(*) AS projects_count, a.songs_id FROM $sTable2 a GROUP BY a.songs_id) bb ON bb.songs_id = b.songsID
        $sWhere
        $sOrder
        $sLimit
    ";
    $rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
    

    To return 0 instead of null in MySQL
    USE

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

    USE with count() having join 2 tables
    like

    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
    

    And Result is

    tblA_Id    Name     SongCount   noOfSong
    -----------------------------------------------------
    7          HSP      NULL        0
    6          MANI     NULL        0
    5          MEET     1           1
    4          user     NULL        0
    3          jaani    2           2
    2          ammy     NULL        0
    1          neha     2           2 
    
    0 讨论(0)
提交回复
热议问题