2014-10-09 42 views
0

沒有數據我的問題是類似這樣的問題:MySQL Query - return value for past 12 months record for each monthMySQL查詢 - 過去4個月,如果返回數據特定月份返回0

但我不能工作,如何與我的查詢實現它。

我需要過去4個月的數據,並且如果當月沒有輸出0的話。

這是我目前的查詢我的笨模型:

function getMonthlySumOrders() 
{ 
    $this->db->select("SUM(price_each*quantity) AS sum_monthly_price, DATE_FORMAT(job.order_date, '%M') AS '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'); 

return $query->result_array(); 
} 

function getMonthlySumCompleted() 
{ 
    $this->db->select("SUM(price_each*quantity) AS 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'); 

return $query->result_array(); 
} 

在查詢返回正確的日期期間的結果,但不顯示0時,有沒有價值的時刻,所以爲了兩組數據之間不同步。

回答

0

試試這個:

if($query-num_rows() > 0) 
{ 
return $query->result_array(); 
} 
else 
{ 
return "0"; 
}