2017-04-12 55 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

感謝您的幫助 –