thinkphp3.2 分组统计连接查询

$count=$reportTempModel->where($where)->count('distinct PID');


$test=$disease->alias('a')->join('left join __DISEASE__ b on a.disease_id = b.id')->field("a.disease_id,b.name,count(a.id) as total")->where($where)->group('disease_id')->select();

生成如下语句:

SELECT a.disease_id,b.name,count(a.id) as total FROM bureau_physical_disease a left join bureau_disease b on a.disease_id = b.id WHERE ( disease_id in (2,3) ) GROUP BY disease_id


例2

 $count=$reportTempModel
                ->alias('a')
                ->join('left join __VILLAGER_ARCHIVES__ b on a.PID = b.PID')
                ->where($where)
                ->count('distinct a.PID');
        $Page=new \Think\Page($count);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $data=$reportTempModel
                ->alias('a')
                ->field('a.type,b.name,b.PID,b.archivesID,b.update_time')
                ->join('left join __VILLAGER_ARCHIVES__ b on a.PID = b.PID')
                ->where($where)
                ->group('a.PID')
                ->select();

例3

$orderM->field('status,count(*) as num')->where(array('buyer_id'=>$member_id))->group('status')->select();


你可能感兴趣的:(php)