使用 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();
来源: http://www.bubuko.com/infodetail-3365032.html