yii2中的复杂子查询

时间:2016-01-06 07:03:16

标签: activerecord subquery yii2

任何人都可以在yii2 ???

中构建此子查询
//*[@id="approvalConfirm"]

帮助很明显!! 在此先感谢!!

2 个答案:

答案 0 :(得分:3)

您可以使用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]);

答案 1 :(得分:-2)

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

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