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

后端 未结 7 1762
轻奢々
轻奢々 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:33

    You might use both IN and BETWEEN statements thru CDbCriteria:

    $criteria = new CDbCriteria();
    $criteria->addInCondition("id", array(6,7,8,9));
    $criteria->addBetweenCondition('id', '10', '20', 'OR');
    $result = User::model()->findAll($criteria);
    

    this will result in SQL query like this:

    SELECT *
    FROM `User`
    WHERE `id`
    IN ( 6, 7, 8, 9 )
    OR `id` BETWEEN 10 AND 20
    

    Note the 4-th paramenter OR in addBetweenCondition() method; missing it, the default AND will be applied to concatenate that condition to the rest of WHERE-query.

    P.S. Strictly speaking those addBetweenCondition() and addInCondition() methods should be added to an existing condition. So, you might need to set first a criteria's initial condition like this:

    $criteria->condition = '1=1';
    

提交回复
热议问题