我有一个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
答案 0 :(得分:1)
$theYear = $this->Model->Database->find('all',array('fields'=>'DISTINCT Year(Report.dated)' )); // this appears to be the correct syntax