我有工作的 sql 查询,它为有报告的每一年返回一行。我需要 cakephp 1.3 控制器的语法来获得相同的结果。
SELECT DISTINCT Year( `dated` )
FROM `reports`
ORDER BY Year( `dated` ) ASC
我尝试过的 cakephp 1.3 查询:
$theYear = $this->Model->Database->find('all',array('fields'=>'DISTINCT Report.dated as Year')); // this still returns all entries, not distinct
或者
$theYear = $this->Model->query('SELECT DISTINCT Report.dated from Report as Year ORDER by Report.dated ASC'); // returns bool(false)
还在我的蛋糕模型中尝试了一个功能:
function getYears()
{
$ret = $this->query
(
"SELECT DISTINCT dated FROM reports as Year ORDER BY dated ASC"
);
return $ret;
} // returns NULL
预期结果是: 2011 2012 2013