使用cakephp 1.3在mysql db table中选择与创建日期不同的年份

时间:2013-05-14 22:02:37

标签: mysql find cakephp-1.3 distinct

我有一个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

1 个答案:

答案 0 :(得分:1)

$theYear = $this->Model->Database->find('all',array('fields'=>'DISTINCT Year(Report.dated)' )); // this appears to be the correct syntax