how to used IN & Between Clause in YII ACtive Record?

后端 未结 7 1733
轻奢々
轻奢々 2021-02-14 10:15

I want write a Following Query in Active record .

SELECT *
FROM `User`
WHERE `UserId`
IN ( 6, 7, 8, 9 ) ;

Thanks

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-14 10:52

    If you want to get your query faster, use Command Builder:

    Yii::app()->db->createCommand()
        ->select('*')
        ->from('user')
        ->where(array('in', 'UserId', array(6, 7, 8, 9)))
        ->queryAll();
    

    To get it via CActiveRecord, use findAllByAttributes

    User::model()
        ->findAllByAttributes(array(
             'UserId' => array(6,7,8,9)
        ));
    

    But it will get full object of User with all associated relations, so it's slower.

提交回复
热议问题