du12197 2017-11-03 09:42
浏览 77

如何使用codeigniter基于值计算总记录数

  id | name | status

   1 |  name | yes

   2 |  name | no 

   3 |  name | yes

How i can count total status with value yes and with value no using active records ? Like count is : yes = 2 , no = 1

This is what i have tried so far

$this->db->select('count(status));
//$this->db->select('count(where status == 'yes')); // not working
$query = $this->db->get();
  • 写回答

3条回答 默认 最新

  • dongxian6653 2017-11-03 09:47
    关注

    Use where method of CI to add where condition.change your query like below:

    $this->db->select('count(status)');
    $this->db->where('status','yes'); // apply where
    $query = $this->db->get();
    

    To get all status count, use group by clause

    $this->db->select('status,count(status)');
    $this->db->group_by('status'); // apply group by
    $query = $this->db->get();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式