多表联查count数据来进行统计,会出现重复的数据,这时候在查询中加上distinct来返回不同的值.
DB::name(‘member’)
->alias(‘a’)
->field(‘count( distinct b.id) as followCount,count(distinct c.id) as articleCount,count(distinct d.id) as readBookCount,count(distinct e.id) as commentsCount’)
->leftJoin(‘member_follow b’,‘a.id = b.uid and b.delete_time is null’)
->leftJoin(‘article c’,‘a.id = c.uid and c.delete_time is null and c.art_status = 1’)
->leftJoin(‘read_book d’,‘a.id = d.uid and d.delete_time is null’)
->leftJoin(‘order_score e’,‘a.id = e.uid and e.delete_time is null’)
->where(‘a.delete_time’,null)
->group(‘a.id’)
->select();
如果需要查看distinct和group by的区别可以去
https://blog.csdn.net/syc001/article/details/72841859
进行观看