在cakephp中使用DATE_SUB(CURDATE(),INTERVAL 2 DAY)条件的方法

时间:2012-01-31 07:54:22

标签: cakephp conditional-statements sql-date-functions

我正在使用cakephp并创建这个条件:

$conditions = array("Userword.levelid"=>0, "Userword.userid"=>$this->Auth->user('UserId'),"date(Userword.date)"=>"DATE_SUB(CURDATE(),INTERVAL 0 DAY)");  
$Userword = $this->paginate(null, $conditions);
使用此选择

蛋糕:

`Userword`.`userwordid`, `Userword`.`userid`, `Userword`.`wordid`, `Userword`.`date`, `Userword`.`levelid`, `Word`.`wordid`, `Word`.`word`, `Word`.`meaning`, `User`.`UserId`, `User`.`Email`, `User`.`password`, `User`.`username`, `User`.`cell`, `User`.`web` FROM `userwords` AS `Userword` LEFT JOIN `words` AS `Word` ON (`Userword`.`wordid` = `Word`.`wordid`) LEFT JOIN `users` AS `User` ON (`Userword`.`userid` = `User`.`UserId`) WHERE date(`Userword`.`date`) = 'DATE_SUB(CURDATE(),INTERVAL 2 DAY)'  

但由此选择mysql无法返回记录,因为此select中的cakephp在最后一段中使用此字符(')。 当我删除此字符并在phpmyadmin中测试此代码时,此代码返回行。下面的两张图片显示了这一点:

cakephp mysql code

image of phpmyadmin without <code>'</code> character

1 个答案:

答案 0 :(得分:9)

只是不要把它分开作为键=&gt;价值对:

$conditions = array(
   ...
   'DATE(Userword.date) = DATE_SUB(CURDATE(), INTERVAL 0 DAY)'
);