Yii2.0中使用Union查询,并使用join,支持分页


 1 $query1 = Class1::find()->where($where);
 2 $query2 = Class1::find()->alias('a')->join('left join', Class2::tableName() . 'as b', 'b.id = a.objId')
        ->select('a.*')->where($where2); 3 $queryAll = $query1->union($query2, true); 4 $query = (new Query())->from(['c' => $queryAll])->select('c.*,d.type,d.title')
        ->join('left join', Class3::tableName() . 'as d', 'd.id = c.objId')->distinct(true)->orderBy(['c.time'=>SORT_DESC]); 5 $sql = $query->createCommand()->getRawSql(); 6 $result = $query->offset(($page - 1) * 10)->limit(10)->all(); 7 8 $totalCount = $query->count(); 9 $pagination = new Pagination(['totalCount'=>$totalCount, 'pageSize'=>10]); 10 $result = $query->offset($pagination->offset)->limit($pagination->limit)->all();

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM