【问题标题】:Kohana3 ORM sum() equivalentKohana3 ORM sum() 等效
【发布时间】:2012-09-17 09:27:29
【问题描述】:

我需要相当于

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

在 Kohana3 中。

那么,如何在 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.

【问题讨论】:

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


    【解决方案1】:

    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(array('SUM("balance")', 'total_balance)) 或类似名称。注意双引号
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多