Skip to content

Commit 4924eaf

Browse files
committed
feat: Added $distinct parameter to SelectQuery::count
1 parent d0db035 commit 4924eaf

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

src/Query/SelectQuery.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,14 @@ public function runChunks(int $limit, callable $callback): void
326326
*
327327
* @psalm-param non-empty-string $column Column to count by (every column by default).
328328
*/
329-
public function count(string $column = '*'): int
329+
public function count(string $column = '*', bool $distinct = false): int
330330
{
331331
$select = clone $this;
332332

333333
//To be escaped in compiler
334-
$select->columns = ["COUNT({$column})"];
334+
$select->columns = [
335+
$distinct === true ? "COUNT(DISTINCT {$column})" : "COUNT({$column})",
336+
];
335337
$select->orderBy = [];
336338
$select->groupBy = [];
337339

tests/Database/Functional/Driver/Common/Schema/TableTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public function testCountDistinct(): void
495495
],
496496
);
497497

498-
$this->assertSame(4, $table->select()->count('DISTINCT(id)'));
498+
$this->assertSame(4, $table->select()->count('id', true));
499499
}
500500

501501
public function setUp(): void

0 commit comments

Comments
 (0)