首页 > 数据库技术 > 详细

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

时间:2020-09-25 16:37:01      阅读:108      评论:0      收藏:0      [点我收藏+]

 

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 左边的表),根据左边的表的类型查询统计对应右表的,显示就结果

 

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

原文:https://www.cnblogs.com/ComputerVip/p/13730970.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!