php - 如何使用 codeigniter 根据值计算记录总数

标签 php mysql codeigniter activerecord

  id | name | status

   1 |  name | yes

   2 |  name | no 

   3 |  name | yes

我如何使用事件记录计算值为 yes 和值为 no 的总状态?像计数是:yes = 2no = 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/

相关文章:

Php Preg_Match 获取数组中的引用字符串

php - MySQL:按子句分组

php - 将数组[]从php发送到mysql

php - codeigniter 显示数据库连接信息

javascript - 来自 2 个不同页面的 Codeigniter 3 多个 Ajax

mysql - 如何触发一个表并更新同一个表?

mysql - MySQL 和 Workbench 中的不同配置

mysql - mysql中的字母数字列自然排序,其中数据是字母、数字、特殊字符的任意组合

codeigniter - 如何在codeigniter中查看查询结果

php - AngularJS 未发布到 Codeigniter Controller