Fetching count of daily records saved using case statement in codeigniter query builder

Viewed 31

I want to get count of records saved on the current date using following approach can anybody help me what is wrong with the code. My Code always returning Zero result first block of case statement is not working for date.

$date = new DateTime("now");

$curr_date = $date->format("Y-m-d");

$this->db->select("case when DATE(created_at) = '$curr_date' then count(*) else 0 end as records_fed_today");

$this->db->from("my_table");

$query = $this->db->get();

return $query->result();
1 Answers

I just fixed my code i was missing group_by clause.

$date = new DateTime("now");
$curr_date = $date->format("Y-m-d");
$this->db->select("case when DATE(created_at) = '$curr_date' then count(*) else 0 end as records_fed_today");
$this->db->from("my_table");
$this->db->group_by('financial_year');
$query = $this->db->get();
return $query->result();           
Related