欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-08 12:54:48
$query1 = Class1::find()->where($where);$query2 = Class1::find()->alias('a')->join('left join', Class2::tableName() . 'as b', 'b.id = a.objId')->selec ......

$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();