mysql left join 查询时主表为null统计count为0的解决方法(join后面加group by)

 

如果没有加group by 则会出UserCount为0外其它都是null

select a.*,count(b.ID) as UserCount from erp_role as a 
    left join erp_user as b on a.ID=b.RoleId where 1=1 and a.TenantID=2 and a.RoleName='string'

 

加上group by一切正常

select a.*,count(b.ID) as UserCount from erp_role as a 
    left join erp_user as b on a.ID=b.RoleId where 1=1 and a.TenantID=2 and a.RoleName='string' GROUP BY a.ID

 

posted @ 2023-04-15 16:12  优六系统(Util6MIS+)  阅读(707)  评论(0)    收藏  举报