当我的列 is_active
为 0
时,我想对字段求和,并且我想将其显示到我的仪表板页面。如果 is_active
为 1
它应该跳过而不是求和。
如何做到这一点?有什么查询?我在 CodeIgniter 中构建了示例查询:
$this->db->select_sum('is_active');
$query = $this->db->get('postfes');
if ($query->num_rows() = 0) {
return $query->num_rows()->is_active;
} else {
return 0;
}
最佳答案
您需要使用where
条件
$this->db->select_sum('is_active'); // the field you want to sum
$this->db->where('is_active', 0)
$data = $this->db->get('postfes')->row();
if ($data) {
return $data->is_active;
} else {
return 0;
}
如果您想计算 is_active = 0 的位置,则使用
$this->db->select('count(*) as total'); // the field you want to sum
$this->db->where('is_active', 0)
$data = $this->db->get('postfes')->row();
if ($data) {
return $data->total;
} else {
return 0;
}
关于mysql - 如果字段值为 0 或 1,如何选择 sum?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59433489/