Aug 25

逆引きrhaco49: あるカラムの総和を取得する

あるカラムの総和を取得するにはsqlでsumを発行するわけですが、rhacoにはDbUtilにsumメソッドがあり、それを利用できます。

$db = new DbUtil(Product::connection());
$sumOfPrice = $db->sum(new Product(),Product::columnPrice());

さらにCriteriaを引数に追加することで、絞りこみした結果の総和を取得できます。


$sumOfPrice = $db->sum(new Product(),Product::columnPrice(),new C(Q::ilike(Product::columnName(),'hoge','w*')));


これはnameフィールドの先頭が「hoge」なものの総和を取得します。

| comment(0)