美文网首页
MySQL-如何使用SUM()并排除SUM()为0的结果 ---

MySQL-如何使用SUM()并排除SUM()为0的结果 ---

作者: 一位先生_ | 来源:发表于2023-08-07 14:07 被阅读0次

    我想显示SUM()的结果,其中结果不是0(零)

    MySQL Table Name:

    • memberlist

    Columns:

    • m_username
    • 2021_03_24
    • 2021_03_17
    • 2021_03_10
    • 2021_03_03
    • 2021_02_24
    • 2021_02_17

    MySQL Query:

    SELECT *, SUM( IFNULL(2021_03_24, 0) + IFNULL(2021_03_17, 0) + IFNULL(2021_03_10, 0) + IFNULL(2021_03_03, 0) + IFNULL(2021_02_24, 0) + IFNULL(2021_02_17, 0) ) AS TOTAL_CONTRIBUTION FROM memberlist GROUP BY m_username ORDER BY TOTAL_CONTRIBUTION DESC`
    
    

    以上MYSQL查询有问题:

    It shows results where `TOTAL_CONTRIBUTION` = 0
    
    

    如何修改MySQL查询,使结果排除TOTAL_CONTRIBUTION=0的任何数据?

    HAVING子句

    SELECT *, 
    SUM( 
       IFNULL(2021_03_24, 0) 
     + IFNULL(2021_03_17, 0) 
     + IFNULL(2021_03_10, 0) 
     + IFNULL(2021_03_03, 0) 
     + IFNULL(2021_02_24, 0) 
     + IFNULL(2021_02_17, 0) 
     ) AS TOTAL_CONTRIBUTION
    FROM memberlist
    GROUP BY m_username 
    HAVING TOTAL_CONTRIBUTION!=0  
    ORDER BY TOTAL_CONTRIBUTION 
    DESC;
    

    相关文章

      网友评论

          本文标题:MySQL-如何使用SUM()并排除SUM()为0的结果 ---

          本文链接:https://www.haomeiwen.com/subject/opsgpdtx.html