I want write a Following Query in Active record .
SELECT *
FROM `User`
WHERE `UserId`
IN ( 6, 7, 8, 9 ) ;
Thanks
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';