Select Kohana3 ORM sum()等价物

Select Kohana3 ORM sum()等价物,select,count,kohana,kohana-3,kohana-orm,Select,Count,Kohana,Kohana 3,Kohana Orm,我需要相当于 SELECT SUM(balance) as "total_balance" FROM users; 在科哈纳 那么,如何在Kohana3中找到users表的balance列的总和呢 $total_balance = ORM::factory ( 'user' )->find ();//I want to change this string to find total_balance to be a sum of the balance column. ORM中没有等价

我需要相当于

SELECT SUM(balance) as "total_balance" FROM users;
在科哈纳

那么,如何在Kohana3中找到
users
表的
balance
列的总和呢

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

您还可以使用:
DB::select(数组('SUM(“balance”),'total_balance))
或类似用法。请注意双引号