Frage

For example, I have table:

id | number
-----------
1  | 1
2  | 1
3  | 1
4  | 2
5  | 2
6  | 3

i need to count unique numbers (1, 2 and 3), that is 3

i have working SQL code:

SELECT COUNT(DISTINCT `number`) AS `total` FROM `some_table`

but i don't know how better to do this with PHPActiveRecord

War es hilfreich?

Lösung

There is a count() function in phpAR, but you can't make it work with DISTINCT. So, I think the easiest is:

$total = SomeModel::first(['select' => 'COUNT(DISTINCT `number`) AS `total`'])->total;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top