Activerecord yii2中的复杂子查询

Activerecord yii2中的复杂子查询,activerecord,subquery,yii2,Activerecord,Subquery,Yii2,有人能用yii2构建这个子查询吗 select name, crt_by, (select name from tbl_employee_master where tbl_employee_master.contact = (select username from tbl_user where tbl_user.id = tbl_dealer_master.crt_by)) as

有人能用yii2构建这个子查询吗

select name, crt_by, (select name from tbl_employee_master 
                       where tbl_employee_master.contact = (select username from tbl_user 
                       where tbl_user.id = tbl_dealer_master.crt_by)) as 
                       employee, district, contact_person, contact,
 (select count(*) from tbl_dealer_post 
  where tbl_dealer_post.fk_user_id = tbl_dealer_master.id) 
  as post_count from tbl_dealer_master 
  where status=1
帮助是可贵的!!
提前谢谢

这里是您可以使用的
activequery

$query= (new Query())->select([
        'name' ,
        'crt_by' ,
        'employee' => (new Query())->select('name')
            ->from('tbl_employee_master')
            ->where([
                '=','tbl_employee_master.contact', (new Query())
                    ->select('username')
                    ->from('tbl_user')
                    ->where('tbl_user.id=tbl_dealer_master.crt_by')
            ]),
        'district',
        'contact_person',
        'contact',
        'post_count' => (new Query())->select('count(*)')
            ->from('tbl_dealer_post')
            ->where('tbl_dealer_post.fk_user_id=tbl_dealer_master.id')
    ])->from('tbl_dealer_master')->where(['status' => 1]);

这是给定问题的完整解决方案

$query= (new Query())->select([
        'name' ,
        'crt_by' ,
        'employee' => (new Query())->select('name')
            ->from('tbl_employee_master')
            ->where([
                '=','tbl_employee_master.contact', (new Query())
                    ->select('username')
                    ->from('tbl_user')
                    ->where('tbl_user.id=tbl_dealer_master.crt_by')
            ]),
        'district',
        'contact_person',
        'contact',
        'post_count' => (new Query())->select('count(*)')
            ->from('tbl_dealer_post')
            ->where('tbl_dealer_post.fk_user_id=tbl_dealer_master.id')
    ])->from('tbl_dealer_master')->where(['status' => 1]);

您想用
activerecord
activequery
回答问题吗?请查看此内容,非常感谢您的及时回复!!我猜您错过了父查询部分中的from子句!!哦,耶。。。谢谢你提醒我…我已经更新了。。如果答案对未来用户有效,请向上投票。。。。。