Aggregate.php
9 months ago
CRUD.php
1 year ago
FromClause.php
4 years ago
GroupByStatement.php
4 years ago
HavingClause.php
3 years ago
InsertInto.php
1 year ago
JoinClause.php
4 years ago
LimitStatement.php
4 years ago
MetaQuery.php
3 years ago
OffsetStatement.php
4 years ago
OrderByStatement.php
1 year ago
SelectStatement.php
4 years ago
TablePrefix.php
4 years ago
UnionOperator.php
4 years ago
WhereClause.php
3 years ago
Aggregate.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\QueryBuilder\Concerns; |
| 4 | |
| 5 | use Give\Framework\QueryBuilder\Clauses\RawSQL; |
| 6 | |
| 7 | /** |
| 8 | * @since 2.19.0 |
| 9 | */ |
| 10 | trait Aggregate |
| 11 | { |
| 12 | /** |
| 13 | * Returns the number of rows returned by a query |
| 14 | * |
| 15 | * @since 4.10.0 Return 0 when no result is null |
| 16 | * @since 2.19.0 |
| 17 | * @param null|string $column |
| 18 | * |
| 19 | * @return int |
| 20 | */ |
| 21 | public function count($column = null) |
| 22 | { |
| 23 | $column = (!$column || $column === '*') ? '1' : trim($column); |
| 24 | |
| 25 | if (empty($this->selects)) { |
| 26 | $this->selects[] = new RawSQL('SELECT COUNT(%1s) AS count', $column); |
| 27 | } else { |
| 28 | $this->selects[] = new RawSQL('COUNT(%1s) AS count', $column); |
| 29 | } |
| 30 | |
| 31 | $result = $this->get(); |
| 32 | return is_null($result) ? 0 : +$result->count; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns the total sum in a set of values |
| 37 | * |
| 38 | * @since 2.19.0 |
| 39 | * @param string $column |
| 40 | * |
| 41 | * @return int|float |
| 42 | */ |
| 43 | public function sum($column) |
| 44 | { |
| 45 | $this->selects[] = new RawSQL('SELECT SUM(%1s) AS sum', $column); |
| 46 | |
| 47 | return +$this->get()->sum; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get the average value in a set of values |
| 52 | * |
| 53 | * @since 2.19.0 |
| 54 | * @param string $column |
| 55 | * |
| 56 | * @return int|float |
| 57 | */ |
| 58 | public function avg($column) |
| 59 | { |
| 60 | $this->selects[] = new RawSQL('SELECT AVG(%1s) AS avg', $column); |
| 61 | |
| 62 | return +$this->get()->avg; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the minimum value in a set of values |
| 67 | * |
| 68 | * @since 2.19.0 |
| 69 | * @param string $column |
| 70 | * |
| 71 | * @return int|float |
| 72 | */ |
| 73 | public function min($column) |
| 74 | { |
| 75 | $this->selects[] = new RawSQL('SELECT MIN(%1s) AS min', $column); |
| 76 | |
| 77 | return +$this->get()->min; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the maximum value in a set of values |
| 82 | * |
| 83 | * @since 2.19.0 |
| 84 | * @param string $column |
| 85 | * |
| 86 | * @return int|float |
| 87 | */ |
| 88 | public function max($column) |
| 89 | { |
| 90 | $this->selects[] = new RawSQL('SELECT MAX(%1s) AS max', $column); |
| 91 | |
| 92 | return +$this->get()->max; |
| 93 | } |
| 94 | } |
| 95 |