2017-04-12 56 views
1

请问我需要一些帮助与cakephp3 mysql不同。蛋糕php截然不同date_format

我的欲望导致SQL:

select distinct(date_format(torokuDate,'%Y')) as year 
from kani_tbl 
where torokuDate >= '2000-01-01' 
order by torokuDate ASC 
limit 1; 

,但我得到错误的结果:

SELECT (date_format((torokuDate), '%Y')) AS `year` 
FROM kani_tbl 
WHERE torokuDate > :c0 
GROUP BY torokuDate 
ORDER BY torokuDate ASC 
LIMIT 1 

我的模型源:

$query = $this->find(); 
$time = $query->func()->date_format([ 
     'torokuDate' => 'identifier', 
     "'%Y'" => 'literal' 
]); 

$yearList = $query->select(['year' => $time]) 
      ->distinct('torokuDate') 
      ->from('kani_tbl ') 
      ->order(['torokuDate' => 'ASC']) 
      ->where(['torokuDate >' => '2000-01-01']) 
      ->limit(1); 
//    ->hydrate(false) 
//    ->toArray(); 
var_dump($yearList); 

请帮我加在不同的领域MySQL命令。

回答

0

作为您期望的查询,您需要明确date_format(torokuDate,'%Y')的结果,而不是'torokuDate'字段。所以,你的代码应该是这样的:

$yearList = $query->select(['year' => $time]) 
     ->distinct() 
     ->from('kani_tbl ') 
     ->order(['torokuDate' => 'ASC']) 
     ->where(['torokuDate >' => '2000-01-01']) 
     ->limit(1); 

如何使用不同的方法:https://api.cakephp.org/3.4/class-Cake.Database.Query.html#_distinct

+0

感谢您的帮助 –