- 使用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();
- 聯表查詢可以寫多個leftjoin,之前以為只能寫一個,實際可以多個表聯查。