mysql 根据where条件,分组,联表等统计数据条数

  1. 使用count可以统计数据量,遇到一个新的需求是有两张表,user 和 photo,一对多关系。要统计photo表里属于该用户的照片的数量,where条件是p.status=1,u.is_delete=0, p.is_delete=0。使用yii方法没有实现,最终是用原生的SQL语句实现的。在此记录一下。
    $sql = "SELECT count(*) as totalNum from beauty_photos as p left join beauty_user as u on p.uid=u.id where p.status=1 and p.is_delete=0 and u.is_delete=0 group by p.uid";
    $count = \Yii::$app->db4->createCommand($sql)->queryAll();

猜你喜欢

转载自www.cnblogs.com/bneglect/p/12148413.html
今日推荐