2017-02-05 79 views
1
$this->db->select('SUM(COALESCE(purchase_unit_cost, 0) * quantity) AS cost, SUM(COALESCE(sale_unit_price, 0) * quantity) AS sales, SUM(COALESCE(purchase_net_unit_cost, 0) * quantity) AS net_cost, SUM(COALESCE(sale_net_unit_price, 0)*quantity) AS net_sales', FALSE); 

if($date){ 
$this->db->where('costing.date', $date); 
} 
$q = $this->db->get('costing'); 
if ($q->num_rows() > 0) { 
      return $q->row(); 
} 

代替我想用它來取代上面的代碼:codigniter數據庫查詢與現有

select SUM(COALESCE(sma_costing.purchase_unit_cost, 0) * sma_costing.quantity) AS cost, SUM(COALESCE(sma_costing.sale_unit_price, 0) * sma_costing.quantity) AS sales, SUM(COALESCE(sma_costing.purchase_net_unit_cost, 0) * sma_costing.quantity) AS net_cost, SUM(COALESCE(sma_costing.sale_net_unit_price, 0) * sma_costing.quantity) AS net_sales 
from sma_costing 
right join sma_sale_items on (sma_sale_items.id=sma_costing.sale_item_id and sma_sale_items.quantity=sma_costing.quantity) 
left join sma_sales on sma_sale_items.sale_id=sma_sales.id 
where sma_costing.date='$date' 

曾試圖笨,但不顯示任何結果

$this->db->select('SUM(COALESCE(purchase_unit_cost, 0) * quantity) AS cost, SUM(COALESCE(sale_unit_price, 0) * quantity) AS sales, SUM(COALESCE(purchase_net_unit_cost, 0) * quantity) AS net_cost, SUM(COALESCE(sale_net_unit_price, 0)*quantity) AS net_sales', FALSE);if ($date) {$this->db->where('costing.date', $date);}elseif ($month) {$this->load->helper('date'); $last_day = days_in_month($month, $year); $this->db->where('costing.date >=', $year.'-'.$month.'-01 00:00:00');$this->db->where('costing.date <=', $year.'-'.$month.'-'.$last_day.' 23:59:59');} if ($warehouse_id) { 
     $this->db->join('sales', 'sales.id=costing.sale_id') 
     ->where('sales.warehouse_id', $warehouse_id); 
    }$q = $this->db->get('costing'); if ($q->num_rows() > 0) { 
     return $q->row();}return false; 

這是代碼,我正在調整它

回答

0

試用$this->db->query()

$this->db->query("SELECT SUM(COALESCE(sma_costing.purchase_unit_cost, 0) * sma_costing.quantity) AS cost, SUM(COALESCE(sma_costing.sale_unit_price, 0) * sma_costing.quantity) AS sales, SUM(COALESCE(sma_costing.purchase_net_unit_cost, 0) * sma_costing.quantity) AS net_cost, SUM(COALESCE(sma_costing.sale_net_unit_price, 0) * sma_costing.quantity) AS net_sales 
       FROM sma_costing 
       RIGHT JOIN sma_sale_items on (sma_sale_items.id=sma_costing.sale_item_id AND sma_sale_items.quantity=sma_costing.quantity) 
       LEFT JOIN sma_sales on sma_sale_items.sale_id=sma_sales.id 
       WHERE sma_costing.date='$date' "); 
+0

結合仍然沒有結果 – Haseebuddin09

+0

做..加起來開始和結束日期的時間(Y)拇指 – Haseebuddin09

+0

快樂幫:) –

0

您可以使用查詢使用這樣的腳本

$this->db->query($query, $bind /* as array */);