代码如下(这是 thinkphp 写的):
return $this->alias('cus')
->join('admin a', 'cus.userID = a.id')
->field('cus.userID,a.cjName,count(*) AS count')
->when($month, function ($query) use ($month) {
$query->whereMonth('cjTime', $month);
},function ($query) {
$query->whereYear('cjTime', date('Y'));
})
->when($brand_id, function ($query) use ($brand_id) {
$query->where('cus.cjbrandID', $brand_id);
})
->when($shop_id, function ($query) use ($shop_id) {
$query->where('cus.cjMenDian', $shop_id);
})
->group('cus.userID')
->order('count desc')
->limit(10)
->select()->each(function ($item) {
// ....
return $item;
})->toArray();