简体   繁体   English

使用Yii Query Builder的SQL条件UNION()

[英]SQL conditional UNION() with Yii Query Builder

i wanted to prepare one SQL query in which i wanted to put UNION query but that UNION query is based on certain condition, is it possible to make using Yii query builder. 我想准备一个SQL查询,在其中我要放置UNION查询,但该UNION查询基于特定条件,是否可以使用Yii查询生成器进行。

below is sample code which i have made with simply DAO but i want to make it in query builder style 以下是我仅用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();

Yes, it sure is. 是的,可以。 Assuming this were your test code: 假设这是您的测试代码:

$command = Yii::app()->db->createCommand();

if(<your if statements here) {
    $command->select('id, username, profile')
}

if(<union if statement>) {
    $command->union()
}

Some sample other lines from the Query Builder page 查询构建器页面中的一些其他示例行

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

But the main one you'll want to look at is union() 但是您主要要看的是union()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM