Kohana3 ORM sum()等价物

时间:2012-09-17 09:27:29

标签: select count kohana kohana-3 kohana-orm

我需要相当于

SELECT SUM(balance) as "total_balance" FROM users;

在Kohana3。

那么,如何在Kohana3中找到balanceusers列的总和?

$total_balance = ORM::factory ( 'user' )->find ();//I want to change this string to find total_balance to be a sum of the balance column.

1 个答案:

答案 0 :(得分:10)

ORM中没有SUM()个等效项。 Kohana ORM没有为本机SQL函数提供更多等价物。

作为解决方法,使用DB::select()DB::expr()类似:

$total_balance = DB::select(array(DB::expr('SUM(`balance`)'), 'total_balance'))
    ->from('users')
    ->execute()
    ->get('total_balance');

制作查询:

SELECT SUM(`balance`) AS `total_balance` FROM `users`