How to use 'IN (1,2,3)' with findAll?

前端 未结 2 550
不思量自难忘°
不思量自难忘° 2021-02-18 21:42

I need to get a couple of Students from the database, and I have their primary keys in a comma-separated string.

Normally using SQL it would be something like:



        
相关标签:
2条回答
  • 2021-02-18 21:52

    You can do it also that way:

    $criteria = new CDbCriteria();
    $criteria->addInCondition("id", array(1,2,3,4));
    $result = Student::model()->findAll($criteria);
    

    and use in array any values you need.

    Aleksy

    0 讨论(0)
  • 2021-02-18 21:53

    You can use findAllByAttributes method also:

    $a=array(1,2,3,4);
    $model = Student::model()->findAllByAttributes(array("id"=>$a));
    
    0 讨论(0)
提交回复
热议问题