简体   繁体   English

yii2在查询中选择NULL值(选择NULL作为列,...)

[英]yii2 select NULL value in query (select NULL as column, …)

Is there a way to select NULL value or an exact string in a query in YII2? 有没有办法在YII2中的查询中选择NULL值或精确字符串? I am trying to join 3 queries and I need the same number of columns queries and something like "select NULL as returned, submitted, amount, NULL as iamount....." Example: 我正在尝试加入3个查询,我需要相同数量的列查询,例如“选择NULL作为返回,提交,数量,NULL作为iamount .....”示例:

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

Yes, you have two ways to do it: 是的,你有两种方法可以做到:

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

Both of selects will do the same, but the second line ( test2 ) is preferable as it is more DBMS-agnostic. 两个选择都将执行相同的操作,但第二行( test2 )更可取,因为它更符合DBMS。

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

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