开发文档 v2.x

聚合运算

更新时间:2024年3月21日 14:08 浏览:425

表模型 提供了以下几种常用的聚合运算

 

count - 统计数量

public function count(string $field = '*'): int

参数:

  • string $field - 字段

返回值:

  • int - 总数

 

代码示例:

$count = Be::getTable('user')->count();

// 等价于
$count = (int)Be::getTable('user')->getValue('COUNT(*)');

 

sum - 求和

public function sum(string $field)

参数:

  • string $field - 字段

返回值:

  • string | null - 和

 

代码示例:

$sum = Be::getTable('user')->sum('age');

// 等价于
$sum = Be::getTable('user')->getValue('SUM(`age`)');

 

min - 最小值

public function min(string $field)

参数:

  • string $field - 字段

返回值:

  • string | null - 最小值

 

代码示例:

$min = Be::getTable('user')->min('age');

// 等价于
$min = Be::getTable('user')->getValue('MIN(`age`)');

 

max - 最大值

public function max(string $field)

参数:

  • string $field - 字段

返回值:

  • string | null - 最大值

 

代码示例:

$min = Be::getTable('user')->max('age');

// 等价于
$max = Be::getTable('user')->getValue('MAX(`age`)');

 

avg - 平均值

public function avg(string $field)

参数:

  • string $field - 字段

返回值:

  • string | null - 最大值

 

代码示例:

$avg = Be::getTable('user')->avg('age');

// 等价于
$avg = Be::getTable('user')->getValue('AVG(`age`)');

 

 

导航