id | name | status
1 | name | yes
2 | name | no
3 | name | yes
我如何使用事件记录计算值为 yes 和值为 no 的总状态?像计数是:yes = 2,no = 1
这是我目前尝试过的
$this->db->select('count(status));
//$this->db->select('count(where status == 'yes')); // not working
$query = $this->db->get();
最佳答案
使用 CI 的 where
方法添加 where 条件。如下更改您的查询:
$this->db->select('count(status)');
$this->db->where('status','yes'); // apply where
$query = $this->db->get();
要获取所有状态计数,请使用 group by 子句
$this->db->select('status,count(status)');
$this->db->group_by('status'); // apply group by
$query = $this->db->get();
关于php - 如何使用 codeigniter 根据值计算记录总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47092573/