【问题标题】:counting number of occurances of a number in mysql -codeigniter计算 mysql -codeigniter 中数字出现的次数
【发布时间】:2015-08-04 05:55:05
【问题描述】:

如何在此表中查找open_id_fk 的出现次数。我在我的模型中使用以下代码。

 $this->db->select('open_id_fk, COUNT(open_id_fk) as total');
 $this->db->group_by('open_id_fk'); 
 $this->db->order_by('total', 'desc'); 
 $query = $this->db->get('voting_ip', 10);
 return $query;

预期输出:

116 - 2 次出现 118 - 1 次出现 119 - 1 次出现

收到的实际输出:

116- 6 次出现 118- 3 次出现 119- 2 次出现

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    只需在查询中将 COUNT(open_id_fk) 更改为 COUNT(*)

     $this->db->select('open_id_fk, COUNT(*) as total');
     $this->db->group_by('open_id_fk'); 
     $this->db->order_by('total', 'desc'); 
     $query = $this->db->get('voting_ip', 10);
    

    【讨论】:

      【解决方案2】:

      使用

      COUNT(*)
      

      而不是

      COUNT(open_id_fk)
      

      会起作用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多