yii中的子查询

时间:2013-11-05 09:52:05

标签: php mysql yii

我正在尝试编写子查询

select * from mob_reg where name in (select name from auth where
parent ="naga");

我按照所有这些答案

[Sub-queries ActiveRecord Yii [如何为CActiveDataProvider的条件设置多个条件?

但是我为foreach()获得了无效的参数。

   $model=new MobReg('search');
$model->unsetAttributes();


$criteria = new CDbCriteria(array(
    'select' => 't.imei, t.emp_name, t.email, t.name',
    'join' => 'INNER JOIN auth ON (t.name = auth.name AND auth.parent = :parent)',
    'group' => 't.id',
    'params' => array(':parent' => 'naga')
)); 
$dataProvider = new CActiveDataProvider('MobReg', array(
        'criteria'=>$criteria,
));

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'mob-reg-grid',
    'dataProvider'=>$dataProvider,
    'filter'=>$model,
    'columns'=>array(
        'imei',
        'emp_name',
                'email',
               'name',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

我需要更换什么?任何的想法?我哪里错了?

我想我面临的问题是引用两个不同的表,一个在子查询中,另一个在主查询中。如何处理这个?

1 个答案:

答案 0 :(得分:2)

如下所示:

$criteria = new CDbCriteria(array(
    'select' => 't.imei, t.emp_name, t.email, t.name',
    'join' => 'INNER JOIN auth ON (t.name = auth.name AND auth.parent = :parent)',
    'group' => 't.id',
    'params' => array(':parent' => 'naga')
));

$dataProvider = new CActiveDataProvider('MobReg', array(
       'criteria' => $criteria,
));

这会对你有用吗?