/**
* 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时数据不显示的问题