6

Is there a way to select NULL value or an exact string in a query in YII2? 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:

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

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.