Question

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

Était-ce utile?

La solution

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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top