2015-11-16 125 views
2

有沒有方法在YII2的查詢中選擇空值或確切的字符串? 我想加入3個查詢和我需要相同數量的列的查詢,並像「選擇NULL作爲返回,提交,金額,NULL作爲iamount .....」 例子:yii2在查詢中選擇空值(選擇NULL作爲列,...)

$subQuery2 = Loan::find()->select('person_id')->where(['sheet_id' => $id]); 

$query2 = new Query();$query2 = new Query(); 
$query2->select(['last_name','first_name','fc.tax','NULL as returned','fc.submitted','fc.amount','NULL as iamount','NULL as interest', 
     'b.month','b.year','b.nb']) 

    ->from('sheet as b') 
    ->join('JOIN', 'fee as fc', 
      'b.id = fc.sheet_id')  
    ->join('JOIN','person','fc.person_id = person.id') 
    ->where(['b.id' => $id]) 
    ->andWhere(['not in', 'person_id', $subQuery2]); 

$query = new Query(); 

$query-> 
select(['last_name','first_name','fc.tax','fi.returned','fc.submitted','fc.amount','fi.amount as iamount','fi.interest', 
     'b.month','b.year','b.nb']) 

    ->from('sheet as b') 
    ->join('RIGHT JOIN', 'fee as fc', 
      'b.id = fc.sheet_id')  
    ->join('JOIN','person','fc.person_id = person.id') 
    ->join('LEFT JOIN','loan as fi', 
     'b.id = fi.sheet_id and fc.person_id = fi.person_id') 
    ->where(['b.id' => $id]) 
    ->union($query2) 
    ->orderBy(['last_name' => SORT_DESC]); 

回答

3

是,你有兩種方法可以做到這一點:

$query = new Query(); 
    $query->select([ 
     new Expression('NULL as test') 
     'test2' => new Expression('NULL'), 
    ])->from('sheet as b'); 

選擇的兩個會做同樣的,但第二線(test2)是優選的,因爲它是更DBMS無關。