Codeigniter加入多个表&和

时间:2017-08-04 22:05:53

标签: mysql codeigniter join

我的数据库上有三个表。 第一个表 - insurance_policy,第二个 - insurance_customer&第三个 - insurance_payments

我试图从具有特定日期范围的政策表中获取数据,同时尝试从客户表中提取客户信息&最后试图获得该政策的付款信息。

这是迄今为止的代码 -

$this->db->select('*');
$this->db->from('insurance_policy');
$this->db->join('insurance_customer', 'insurance_customer.customer_id = insurance_policy.Customer_ID', 'left');
$this->db->join('insurance_payments', 'insurance_payments.Policy_Number = insurance_policy.Policy_ID', 'left');
$this->db->where('insurance_policy.Payment_Date >=', $from);
$this->db->where('insurance_policy.Payment_Date <=', $to);

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

我试图从付款表中获取金额总和列,而不是返回所有记录。由于付款数据可以是具有相同政策ID的多个,因此我会得到多个付款数据结果,如下面的结果。相反,我试图为每个策略ID获得一个结果(由于多次支付该策略ID而没有重复)&amp;该政策的付款表金额总和。

先谢谢。

Array
(
[0] => Array
    (
        [Policy_ID] => 26
        [Policy] => 1234
        [Customer_ID] => 30
        [Effective_Date] => 06/08/2016
        [Expiration_Date] => 06/08/2017
        [Insurer] => Lorem
        [Billing] => Lorem
        [Agent] => Lorem
        [Notes] => Lorem
        [Mail_Date] => 
        [Callback_Date] => 
        [Contact_Date] => 
        [Type] => Lorem
        [Policy_Total] => 140
        [Commission_Amount] => 0
        [Brokerage_Fee] => 0
        [Insurance_Price] => 140
        [Payment_Date] => 06/08/2016
        [Cancellation_Date] => 
        [Paid_in_Full] => 0
        [customer_id] => 30
        [First_Name] => Lorem
        [Last_Name] => Lorem
        [Company_Name] => Lorem
        [Company_Type] => Lorem
        [EIN_Number] => 
        [Fee_License] => 1234
        [Address] => Lorem
        [City] => Lorem
        [State] => CA
        [Zipcode] => 12345
        [Phone] => 123-456-7890
        [Alt_Number] => 
        [Fax_Number] => 
        [Email] => user@hotmail.com
        [id] => 8
        [Policy_Number] => 26
        [Date] => 08/16/2017
        [Amount] => 400
        [status] => 1
    )

[1] => Array
    (
        [Policy_ID] => 26
        [Policy] => 1234
        [Customer_ID] => 30
        [Effective_Date] => 06/08/2016
        [Expiration_Date] => 06/08/2017
        [Insurer] => Lorem
        [Billing] => Lorem
        [Agent] => Lorem
        [Notes] => 
        [Mail_Date] => 
        [Callback_Date] => 
        [Contact_Date] => 
        [Type] => Lorem
        [Policy_Total] => 140
        [Commission_Amount] => 0
        [Brokerage_Fee] => 0
        [Insurance_Price] => 140
        [Payment_Date] => 06/08/2016
        [Cancellation_Date] => 
        [Paid_in_Full] => 0
        [customer_id] => 30
        [First_Name] => Art
        [Last_Name] => Lorem
        [Company_Name] => Lorem
        [Company_Type] => Lorem
        [EIN_Number] => 
        [Fee_License] => 55555
        [Address] => Lorem
        [City] => Lorem
        [State] => CA
        [Zipcode] => 12345
        [Phone] => 333-444-5555
        [Alt_Number] => 
        [Fax_Number] => 
        [Email] => user@hotmail.com
        [id] => 4
        [Policy_Number] => 26
        [Date] => 08/15/2017
        [Amount] => 200.10
        [status] => 1
    )
)

1 个答案:

答案 0 :(得分:0)

也许

$this->db->select('SUM(insurance_policy.Policy_Total)');//changed
$this->db->from('insurance_policy');
$this->db->join('insurance_customer', 'insurance_customer.customer_id = 
insurance_policy.Customer_ID', 'left');
$this->db->join('insurance_payments', 'insurance_payments.Policy_Number = insurance_policy.Policy_ID', 'left');
$this->db->where('insurance_policy.Payment_Date >=', $from);
$this->db->where('insurance_policy.Payment_Date <=', $to);
$this->db->group_by("Policy_ID");//added

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