当前位置:首页 > 数据库 > 正文

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

2020-02-11 数据库

  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();

     

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/SQL/13991.html