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

$query1 = Class1::find()->where($where);
$query2 = Class1::find()->alias('a')->join('left join', Class2::tableName() . 'as b', 'b.id = a.objId')->select('a.*')->where($where2);
$queryAll = $query1->union($query2, true);
$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]);
$sql = $query->createCommand()->getRawSql();
$result = $query->offset(($page - 1) * 10)->limit(10)->all();

$totalCount = $query->count();
$pagination = new Pagination(['totalCount'=>$totalCount, 'pageSize'=>10]);
$result = $query->offset($pagination->offset)->limit($pagination->limit)->all();

猜你喜欢

转载自www.cnblogs.com/caohongchang/p/11058399.html