明天一切美好 2018-10-26 17:09:02 2586次浏览 1条回复 0 0 0
$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') 就对了

  • 回复于 2018-12-17 09:49 举报

    没细看去分析,但是通常检查自己的查询语句是否有错,都是直接打印查询的sql,yii的debug很容易就能看到

您需要登录后才可以回复。登录 | 立即注册