Yii left join 查出来的数量与原生 sql left join 查出来的数量不一致 [ 求助交流 ]
$result = "SELECT
*
FROM
`cloud_video` `vd`
LEFT JOIN cloud_video_GroupClass AS vg ON `vd`.`id` = `vg`.`cv_id`
LEFT JOIN cloud_group_class AS gp ON `vg`.`gc_id` = `gp`.`id`
LEFT JOIN cloud_organization AS og ON `gp`.`venue_id` = `og`.`id`
LEFT JOIN cloud_course AS al ON `gp`.`course_id` = `al`.`id`
WHERE
(`vd`.`type` = 2)
AND (`vd`.`status` = 1)";
$rows = Yii::$app->db->createCommand($result)
->queryAll();
$count = Video::find()->where(['type' => 2, 'status' => 1])->count();
echo '<pre>';
var_dump(count($rows), $count);exit;
// int(34)
//string(2) "34"
这个是没问题的
$result = Video::find()->alias('vd')
// ->select('vd.id as vid,vg.id as vid, gp.id as gid, og.id as oid, al.id as aid')
->select('*')
->leftJoin('cloud_video_GroupClass AS vg', '`vd`.`id` = `vg`.`cv_id`')
->leftJoin('cloud_group_class AS gp', '`vg`.`gc_id` = `gp`.`id`')
->leftJoin('cloud_organization AS og', '`gp`.`venue_id` = `og`.`id`')
->leftJoin('cloud_course AS al', '`gp`.`course_id` = `al`.`id`')
->where(['`vd`.`type`' => 2, '`vd`.`status`' => 1])
->asArray()
->all();
$count = Video::find()->where(['type' => 2, 'status' => 1])->count();
echo '<pre>';
var_dump(count($result), $count);exit;
//int(18)
//string(2) "34"
用 yii 提供的 left join 是不正确的,但是把 ->select('*')
换成 ->select('vd.id as vid,vg.id as vid, gp.id as gid, og.id as oid, al.id as aid')
就对了
明天一切美好
注册时间:2016-05-30
最后登录:2019-09-25
在线时长:6小时12分
最后登录:2019-09-25
在线时长:6小时12分
- 粉丝0
- 金钱15
- 威望0
- 积分75