db::sum PHP Method

sum() static public method

Gets the sum of values in a column of a table
static public sum ( string $table, string $column, mixed $where = null ) : mixed
$table string The table name
$column string The name of the column
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
return mixed
    static function sum($table, $column, $where = null)
    {
        $sql = 'SELECT SUM(' . $column . ') AS sum FROM ' . self::prefix($table);
        if (!empty($where)) {
            $sql .= ' WHERE ' . self::where($where);
        }
        $result = self::query($sql, false);
        $result = self::fetch($result);
        return a::get($result, 'sum', 0);
    }

Usage Example

示例#1
0
文件: DbTest.php 项目: nsteiner/kdoc
 public function testSum()
 {
     $this->assertEquals(6, db::sum('users', 'id'));
 }
All Usage Examples Of db::sum