본문 바로가기
MySql

MySQL cakephp에서 최신 기록을 찾는 방법은 무엇입니까?

by 베이스 공부 2020. 12. 28.
반응형

request_time 열이있는 테이블이 있습니다. 여러 요청이있을 것이며 특정 사용자 이름에 대한 최신 요청을 받고 싶습니다.

$this->Requests->find('all', 'conditions' => array (
                                               'username' => $username, 
                                               'MAX'=> 'request_time'));

위는 작동하지 않습니다. Cakephp에 뭔가가 있습니까 아니면 내 자신의-> query ()해야합니까?

 

해결 방법

 

다음을 사용할 수 있습니다.

$this->Requests->find('first', array('conditions' => array('username' => $username), 
                               'order' => array('id' => 'DESC') ));

여기서 id 는 자동 증분 기본 키입니다. find 메소드에서 first 를 사용하는 경우 최신 (단일) 레코드를 제공하거나 대신 all 을 사용합니다.

기본 키를 사용하지 않는 경우 다음을 시도 할 수 있습니다.

$this->Requests->find('first', array('conditions' => array('username' => $username), 
                               'order' => array('request_time' => 'DESC') ));

 

참조 페이지 https://stackoverflow.com/questions/17454062

 

 

반응형

댓글