2012-11-20 31 views
0

我想準備一個SQL查詢,我想把UNION查詢,但UNION查詢是基於一定的條件,是否有可能使使用Yii查詢生成器。下面SQL條件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(); 
+0

注意的主要原因之一,這也是一個不錯的閱讀文檔的想法,它非常清晰,並且寫得很好。 – acorncom

回答

1

是的,它肯定是。假設這是你的測試代碼:

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

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

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

一些樣品從Query Builder page

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

其他行,但你會希望看就是union()

+0

謝謝它的作品... –