ThinkPHP 解决count(id) 为0时查询数据不显示

/**
     * name:# 获取营销排行榜
     * user:lss
     * time:2024/6/28 9:38
     */
    public function getMarketRanking()
    {

        # 获取用户ID
        $user_id = $this->auth->id;

        # 获取分页 类型 项目 排序信息
        $get = $this->request->get();
        $project_id = $get['project_id'] ?? 0;

        # 获取查询条件
        $where_map = [
            'his'=>"staff.user_id=his.share_id",
            'buy'=>"staff.user_id=buy.share_id and buy.status='paid'",
            'tui'=>"staff.user_id=buy.share_id and tui.status='refunded'",
        ];
        if(!empty($project_id)){
            $where_map['his'] .= " and his.project_id={$project_id}";
            $where_map['buy'] .= " and buy.project_id={$project_id}";
            $where_map['tui'] .= " and tui.project_id={$project_id}";
        }

        # 获取统计语句
        $field = "staff.id,staff.user_id,staff.name,staff.mobile";
        $count_field = "count(his.id) as his_num ,count(buy.id) as buy_num ,count(tui.id) as tui_num";

        # 引入模型
        $staffModel = new Staff();

        # 获取员工列表
        $staff_list = $staffModel
            ->alias('staff')
            ->join('miniform_history his',$where_map['his'],'LEFT')
            ->join('miniform_order buy',$where_map['buy'],'LEFT')
            ->join('miniform_order tui',$where_map['tui'],'LEFT')
            ->field($field)
            ->field($count_field)
            ->order('buy_num desc,his_num desc,tui_num asc')
            ->group('staff.id')     // 关键在这里 group主表的主键后 count后不显示的数据就显示了
            ->select();

        $this->success('ok',$staff_list);

    }

总结: group(主表,主键) 可以避免count为0时数据不显示的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值