codeigniter - MySQL Query - Return data for past 4 months and if no data for a particular month return 0 -
codeigniter - MySQL Query - Return data for past 4 months and if no data for a particular month return 0 -
my question similar question: mysql query - homecoming value past 12 months record each month
but can't work out how implement query.
i require info previous 4 months , if there none output 0 month.
here current queries in codeigniter model:
function getmonthlysumorders() { $this->db->select("sum(price_each*quantity) sum_monthly_price, date_format(job.order_date, '%m') 'order_date', customer.company_name", false); $this->db->join('job', 'job.job_id = job_details.job_id'); $this->db->join('customer', 'customer.company_id = job.company_id'); $this->db->where('job.company_id', $this->uri->segment(3)); $this->db->where('month(job.order_date)<=', date("m")); $this->db->where('month(job.order_date)>=', date("m")-3); $this->db->group_by('month(job.order_date)'); $this->db->order_by('job.order_date', "asc"); $query = $this->db->get('job_details'); homecoming $query->result_array(); } function getmonthlysumcompleted() { $this->db->select("sum(price_each*quantity) sum_monthly_price", false); $this->db->join('job', 'job.job_id = job_details.job_id'); $this->db->join('customer', 'customer.company_id = job.company_id'); $this->db->where('job.company_id', $this->uri->segment(3)); $this->db->where('month(job.completed_date)<=', date("m")); $this->db->where('month(job.completed_date)>=', date("m")-3); $this->db->group_by('month(job.completed_date)'); $this->db->order_by('job.completed_date', "asc"); $query = $this->db->get('job_details'); homecoming $query->result_array(); }
at moment query returning results right date period doesn't show 0 when there no values, , order between both sets of info goes out of sync.
try this:
if($query-num_rows() > 0) { homecoming $query->result_array(); } else { homecoming "0"; }
mysql codeigniter
Comments
Post a Comment