如何使用 IN & YII Active Record 中的 Between 子句?

发布于 2024-11-08 18:28:30 字数 115 浏览 0 评论 0 原文

我想在活动记录中编写以下查询。

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

谢谢

I want write a Following Query in Active record .

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

从﹋此江山别 2024-11-15 18:28:30

您可以使用 CDbCriteria 语句:

$criteria = new CDbCriteria();
$criteria->addInCondition('userId', array(6,7,8,9));
$result = User::model()->findAll($criteria);

You can use CDbCriteria statement:

$criteria = new CDbCriteria();
$criteria->addInCondition('userId', array(6,7,8,9));
$result = User::model()->findAll($criteria);
以为你会在 2024-11-15 18:28:30

您可以将数组作为特定属性的值,如下所示(未经测试):

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

You can put your array as a value for a specific attribute, like this (no tested):

$model=new User();
$result=$model->findAllByAttributes(array('UserId'=>array(6,7,8,9)));
作死小能手 2024-11-15 18:28:30

如果您想更快地获得查询,请使用 Command Builder:

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

要通过 CActiveRecord 获取它,请使用 findAllByAttributes

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

但它将获得具有所有关联关系的 User 的完整对象,因此速度较慢。

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.

戏舞 2024-11-15 18:28:30

您可以通过 CDbCriteria 使用 IN 和 BETWEEN 语句:

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

这将导致如下 SQL 查询:

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

注意 第 4 个参数 OR /www.yiiframework.com/doc/api/1.1/CDbCriteria#addBetweenCondition-detail" rel="nofollow noreferrer">addBetweenCondition() 方法;如果缺少它,将应用默认的 AND 将该条件连接到 WHERE 查询的其余部分。

PS 严格来说,这些 addBetweenCondition()addInCondition() 方法应该添加到现有条件中。因此,您可能需要首先设置标准的初始条件,如下所示:

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

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';
云之铃。 2024-11-15 18:28:30

我仍然使用这种方式:

public function getInterval( $data_start, $data_stop ){
  $criteria = new CDbCriteria;
  $criteria->condition = "date  >= '$data_start' AND date <= '$data_stop'";
  return $criteria;
}
$model = Object::model()->findAll(getInterval('2014-06-01','2014-06-30');

I still using this way:

public function getInterval( $data_start, $data_stop ){
  $criteria = new CDbCriteria;
  $criteria->condition = "date  >= '$data_start' AND date <= '$data_stop'";
  return $criteria;
}
$model = Object::model()->findAll(getInterval('2014-06-01','2014-06-30');
无所的.畏惧 2024-11-15 18:28:30

yii 中有一个名为 findAllBySql 的函数来运行 sql 查询并获取输出。

$sql="SELECT * FROM `User` WHERE `UserId` IN ( 6, 7, 8, 9 )";//Your Sql query
$value=User::model()->findAllBySql($sql);// "User" is the model belongs to the table 'User'

“$value”将以数组形式返回结果。要获取这些值,您可以使用以下方法。

foreach($value as $val){
   echo $val->UserId;
}

(或者)

var index=0;
echo $val[$index]->UserId;

There is an function called findAllBySql in yii to run sql query and get the outputs.

$sql="SELECT * FROM `User` WHERE `UserId` IN ( 6, 7, 8, 9 )";//Your Sql query
$value=User::model()->findAllBySql($sql);// "User" is the model belongs to the table 'User'

The "$value" will return the result in a array. To get the values you can use the following method.

foreach($value as $val){
   echo $val->UserId;
}

(or)

var index=0;
echo $val[$index]->UserId;
情仇皆在手 2024-11-15 18:28:30

您可以使用addInCondition,也可以使用compare方法。

 $criteria = new CDbCriteria();
 $criteria->compare('UserId',array(6,7,8,9));
 $userDataObj = User::model()->findAll($criteria);

Either you can use addInCondition or you can also use compare method.

 $criteria = new CDbCriteria();
 $criteria->compare('UserId',array(6,7,8,9));
 $userDataObj = User::model()->findAll($criteria);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文