2018-10-26 17:10:12 2703次浏览 1条回答 0 悬赏 10 金钱
$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-10-26 17:10

来大神讲解下

补充于 2018-10-26 17:10

来大神讲解下

最佳答案

  • wsd15321 发布于 2018-10-26 17:26 举报

    yii\db\ActiveQuery里的public function populate($row)方法看看就懂了

    1 条回复
    回复于 2018-10-26 18:19 回复

    明白了,这个问题之所以产生还是因为对php的pdo的left join不熟导致的,2个相同的表,如果里面都有相同的字段,那么后面的字段回覆盖前面的字段内容,然而在MySQL中运行,MySQL回用id1,id2给标识出来

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
明天一切美好
职场新人

明天一切美好

注册时间:2016-05-30
最后登录:2019-09-25
在线时长:6小时12分
  • 粉丝0
  • 金钱15
  • 威望0
  • 积分75

热门问题