Skip to content

Commit

Permalink
✨ 新增(Query.php):新增avg()和sum()方法以計算平均值和總和
Browse files Browse the repository at this point in the history
這些變更是為了擴展Query類別的功能,讓使用者能夠更方便地計算指定欄位的平均值和總和,從而提高查詢的靈活性和效率。
  • Loading branch information
mathsgod committed Jan 14, 2025
1 parent 007ba6c commit 50eb9cd
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ public function collect()
return $this->execute();
}

public function avg(string $column)
{
$c = clone $this;
$c->columns([
"c" => new Expression("avg($column)")
]);
$sql = $c->getSqlString($this->schema->getPlatform());
return $this->schema->query($sql)->fetchColumn(0);
}

public function sum(string $column)
{
$c = clone $this;
$c->columns([
"c" => new Expression("sum($column)")
]);
$sql = $c->getSqlString($this->schema->getPlatform());
return $this->schema->query($sql)->fetchColumn(0);
}

public function max(string $column)
{
$c = clone $this;
Expand Down

0 comments on commit 50eb9cd

Please sign in to comment.