I have a table with the following info:
id | user_id | points
--------------------------
1 | 12 | 48
2 | 15 | 36
3 | 18 | 22
4 | 12 | 28
5 | 15 | 59
6 | 12 | 31
etc.
What I want is a top 10 (array) with most entries per user_id (order high to low).
So using the table above I need the following array in return:
- 12 => 3 rows
- 15 => 2 rows
- 18 => 1 row
- etc.
How can I do this with CodeIgniter using the active record query method? Can this be done with COUNT and GROUP BY user_id?
I believe you’ll want something like this:
This will produce a result like
UPDATE: As some pointed out in the comments the original query was summing the user_ids rather than counting them. I’ve updated the active record query to correct this.