Yii Active Record功能BETWEEN

时间:2012-11-01 00:02:24

标签: php yii

在Yii Framework中使用BETWEEN的正确方法是什么?

代码:

...
$criteria = new CDbCriteria;
$criteria->condition = 'datetime BETWEEN '.$datetimemin.' AND '.$datetimemax;
$user = Users::model()->find($criteria); 
...

错误:

SQL: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '07:10:10 AND 2011-10-10 13:10:10)' at line 1. 

2 个答案:

答案 0 :(得分:12)

最正确的

$criteria->addBetweenCondition('attributeName', 'value1', 'value2');

答案 1 :(得分:2)

我不知道在Yii中这样做的“正确”方法。

但错误是因为日期需要引用。

$criteria->condition = 'datetime BETWEEN "'.$datetimemin.'" AND "'.$datetimemax.'"';