0

亲爱的所有朋友,我是 Codeigniter 框架的新手,我希望通过选择两个表数据来获得总金额,问题是当我在第二个表中输入数据时,总金额会翻倍。

function total_amount($booking_no = NULL)
{
    $data  = array('forwarding_cargo_booking_details.*',
                   'other_charges.client_amount');


    $this->db->select($data);
    $this->db->where('forwarding_cargo_booking_details.booking_no',$booking_no);
    $this->db->join('other_charges','forwarding_cargo_booking_details.booking_no = other_charges.booking_no','left');
    $this->db->select('sum((`bk_m3`*`o_freight_client`)*(`selling_rate`)+`pod_client`+`thc_client`+`caf_client`+`baf_client`+`haulage_client`+`war_risk_client`+`warehouse_client`+`thc_dest_client`+`pp_surcharge_client`+`doc_charges_client`+`client_amount`) as salam', FAlSE);
    $query = $this->db->get('forwarding_cargo_booking_details');            
    if($query->num_rows() > 0)
    {
        return $query->row();
    }
}
4

2 回答 2

1

改成这样。这里使用派生查询并测试它是否工作正常

function total_amount($booking_no = NULL)
{
    $sql_query  =   "SELECT
                        forwarding_cargo_booking_details.*,
                        other_charges.client_amount,
                        sum((`bk_m3`*`o_freight_client`)*(`selling_rate`)+`pod_client`+`thc_client`+`caf_client`+`baf_client`+`haulage_client`+`war_risk_client`+`warehouse_client`+`thc_dest_client`+`pp_surcharge_client`+`doc_charges_client`+`client_amount`) as amount
                    FROM forwarding_cargo_booking_details 
                    LEFT JOIN (SELECT booking_no , sum(client_amount) FROM other_charges group by booking_no) as other_charges ON forwarding_cargo_booking_details.booking_no = other_charges.booking_no
                    ";
    $query = $this->db->query();   

    if($query->num_rows() > 0)
    {
        return $query->row();
    }
}
于 2012-10-09T06:21:35.887 回答
0

使用此代码打印查询,并检查查询。

$this->db->last_query();

我认为您忘记在查询中使用“分组依据”

于 2012-10-08T07:07:54.673 回答