需要计算一个 id 有多少条记录 return,codeigniter

Need to count how many records were return for an id, codeigniter

我有一票table持有选票。我需要根据每个用户的 id 获得每个用户在 table 中的投票数,然后按投票最多的人对查询进行排序。基本上是领导者table。我该怎么做?

假设存储所有选票的 table 称为 activity,它存储 iduser_id

获得总票数:

select user_id, count(user_id) as votes from activity group by user_id;

获得前 5 名投票者:

select user_id, count(user_id) as votes from activity group by user_id ORDER BY votes desc LIMIT 5;
$this->db->select('COUNT(user_id) AS total_votes');
$this->db->from('votes');
$this->db->group_by('user_id');
$this->db->order_by('total_votes DESC');
$result = $this->db->get()->result();