0

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

4

1 回答 1

0

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;
于 2013-02-22T16:11:40.613 回答