2010年02月18日
CakePHP findでMAX、MIN
CakePHPのfindでMAX、MIN関数などを使う場合は、
fieldsキーに指定します。
サンプルデータ:
+----+--------+ | id | number | +----+--------+ | 1 | 100 | | 2 | 10 | | 3 | 1000 | +----+--------+
サンプルコード:
$result = $this->Example->find('first', array("fields" => "MAX(Example.number) as max_number"));
$resultの出力結果:
Array
(
[0] => Array
(
[max_number] => 1000
)
)
SUM(集計)も同様に指定できます。
サンプルコード:
$result = $this->Example->find('first', array("fields" => "SUM(Example.number) as sum_number"));
$resultの出力結果:
Array
(
[0] => Array
(
[sum_number] => 1110
)
)
実行環境
CentOS 5.3
CakePHP 1.3.0-beta
CentOS 5.3
CakePHP 1.3.0-beta
