Mysql 如何在cakephp中查找最新记录?
我有一张表,上面有一列“请求时间”。将有多个请求,我想获取特定用户名的最新请求:Mysql 如何在cakephp中查找最新记录?,mysql,cakephp,Mysql,Cakephp,我有一张表,上面有一列“请求时间”。将有多个请求,我想获取特定用户名的最新请求: $this->Requests->find('all', 'conditions' => array ( 'username' => $username, 'MAX'=> 'request_time
$this->Requests->find('all', 'conditions' => array (
'username' => $username,
'MAX'=> 'request_time'));
上述方法不起作用。Cakephp中有什么东西,或者我需要自己做->查询()吗 如果您有自动递增的主键,则可以找到此表的最新记录
$this->Requests->find('find', 'conditions' => array ('username' => $username),
'order' => array('id' => 'DESC')
);
您可以使用以下选项:
$this->Requests->find('first', array('conditions' => array('username' => $username),
'order' => array('id' => 'DESC') ));
$this->Requests->find('first', array('conditions' => array('username' => $username),
'order' => array('request_time' => 'DESC') ));
其中,id
是自动递增的主键。
如果您在find方法中使用first
,或者使用all
,这将为您提供最新(单个)记录
如果未使用主键,可以尝试以下操作:
$this->Requests->find('first', array('conditions' => array('username' => $username),
'order' => array('id' => 'DESC') ));
$this->Requests->find('first', array('conditions' => array('username' => $username),
'order' => array('request_time' => 'DESC') ));
你们有自动递增的主键吗?这更多的是关于基于时间的查询,但我可以看到如何使用ID。