SQL conditional UNION() with Yii Query Builder



我想准备一个SQL查询,我想在其中放置UNION查询,但该UNION查询基于某些条件,是否可以使用Yii查询生成器进行。

下面是我用简单的 DAO 制作的示例代码,但我想以查询构建器风格制作它

    if(isset($first) && $first!="")
       $con .= "SELECT id,first, from user where first LIKE '%".$first."%'";
   if(isset($last) && $last!="")
       $con .= " AND last LIKE '%".$last."%'";
   if((!empty($first) || !empty($last)) && (!empty($flag)) )
       $con .= " UNION SELECT id,first, from user where flag = 1"
   $command    = $connection->createCommand($con)->queryall();

是的,确实如此。 假设这是您的测试代码:

$command = Yii::app()->db->createCommand();
if(<your if statements here) {
    $command->select('id, username, profile')
}
if(<union if statement>) {
    $command->union()
}

"查询生成器"页面中的一些示例其他行

->from('tbl_user u')
->join('tbl_profile p', 'u.id=p.user_id')
->where('id=:id', array(':id'=>$id))
->queryRow();

但是您要查看的主要内容是union()

最新更新