Temporary Tables - Querybuilder

Hi, I need to do this query in Yii2 and I could find the solution using query builder

MYSQL


select employee_id, sum(amount)

from

(

select amount, employee_id, status from company_expense

union

select amount, employee_id, status from expense

) as expenses

where status = 1

group by employee_id

YII


$query = new Query;

$query->select('amount, employee_id, status')->from('company_expense');


$anotherQuery = new Query;

$anotherQuery->select('amount, employee_id, status')->from('expense');


$query->union($anotherQuery);

I need to create a new query to group by employee_id. Is there any way to make a select from a temporary table ($QUERY)?

Thanks!

I think you cannot do this with the Yii2.0 I didn’t find any methods for it, or how create custom sql command for it.

YO DAWG

I HEARD YOU LIKE SQL


$query = new Query;

$query->select('amount, employee_id, status')->from('company_expense');


$anotherQuery = new Query;

$anotherQuery->select('amount, employee_id, status')->from('expense');


$query->union($anotherQuery);

SO I PUT A QUERY IN YO QUERY


$qiang_will_kill_me = new \yii\db\Query;

$qiang_will_kill_me->select(['SUM(t.amount) as gotcha']);

$qiang_will_kill_me->from('(' . $query->createCommand()->getRawSql() . ') as t');

SO U CAN SELECT WHILE U SELECT

Just added sub-query support: https://github.com/yiisoft/yii2/blob/master/docs/guide/query-builder.md#from

Wow that was fast

Lovely…

Amazing!!

Thank you all!