How to use Sub query in from clause ?

How can I manage to build a complex query like following, using Yii2 ActiveRecord :


Select A.* from (select * from table where table.condition = 1) as A

A basic example of what I’m trying to achieve :




//Subject = ActiveRecord Class

$query = Subject::find()->joinWith('relation');

$query->select['val'] = 'relation.name';

$query->groupBy('relation.id');


//The following line is wrong but you get the idea

$mainQuery = Subject::find()->from($query. ' C');


$mainQuery->select['value'] = 'count(C.*)';

return $mainQuery->asArray()->all();



If anybody have an idea of how to achieve that, it will be awesome

Ok I found the answer :


$mainQuery = Subject::find()->from(['C' => $query]);