PostgreSQL统计每天的记录数量,没有记录就为0或返回null


 

1. 需求统计下面事件类型的数量,没有此类型没有数据的返回为null,前端显示为0,请写出PostgreSQL的sql

 

SQL语句是:

    select t.event_code as eventCode,t.event_name as eventName,s.eventNum from event_type t LEFT JOIN 
        (select s.parent_code
        as eventCode,count(id) as eventNum
        from article s        
            where 1=1 
        group by s.parent_code order by eventNum desc) s on s.eventCode=t.event_code where t.parent_code='0'

显示结果:

 

解析原理:

在实际开发中我们经常遇见统计每天的记录数量,没有记录就为0或返回null。

以主要查询的显示的表为主表(LEFT JOIN 左边的表),根据左边的表的类型查询统计对应右表的,显示就结果

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM