在yii中添加条件

时间:2015-11-27 09:37:25

标签: yii

如何在Criteria中的Join Query中编写AddNotin条件,

public function assigned()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;                              
    $criteria->addCondition("t.tile_id <> (INNER JOIN `tbl_user_tile` AS `pr` ON pr.tile_id = t.tile_id");                
    $criteria->compare('deleted_by',$this->deleted_by,true);
    $criteria->compare('deleted_date',$this->deleted_date,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

我想返回不匹配记录它不工作我的代码有什么问题? 我想在第二张表记录中没有匹配的第一张表记录。

2 个答案:

答案 0 :(得分:0)

最后我得到了输出,

public function assigned()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

            $classes = Question::model()->findAll(array(   
'join' => 'INNER JOIN `tbl_user_tile` AS `pr` ON pr.tile_id = t.tile_id'));


            $classarray =   "";
            for($i=0;$i<count($classes);$i++)
            {
            $classarray .= $classes[$i]['tile_id'].",";                
            }

            $classarray    = rtrim($classarray,',');               
            $criteria=new CDbCriteria;

            $criteria->addCondition("t.tile_id  not in (" . $classarray . ")");     

    $criteria->compare('deleted_by',$this->deleted_by,true);
    $criteria->compare('deleted_date',$this->deleted_date,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

答案 1 :(得分:0)

您可以按如下方式更改查询:

public function assigned()
{
    // @todo Please modify the following code to remove attributes that should not be searched.
    $criteria = new CDbCriteria;
    $criteria->join = "LEFT JOIN `tbl_user_tile` AS `pr` ON pr.tile_id = t.tile_id";
    $criteria->condition = "pr.tile_id IS NULL";

    $criteria->compare('deleted_by',$this->deleted_by,true);
    $criteria->compare('deleted_date',$this->deleted_date,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

希望这会有所帮助。如果有任何疑虑,请告诉我。