SQL Join SUM无法正常工作

时间:2017-09-25 09:01:17

标签: php mysql codeigniter

我在使用查询生成结果时遇到问题。我想从另一个与主表相关的表中生成行结果的SUM。

我未能输出的是付款总额和应付款额。

  • 问题#1 - 我无法生成付款的正确输出。该 查询似乎产生了numrows * sum_of_payments

  • 的公式
  • 问题#2 - 我无法产生应付总额的输出。

我正在使用Codeigniter 3,这是我的模特。

function fetch_billing_records($case_id, $patient_id, $status) {

        $this->db->join('cases', 'cases.id = billing.case_id', 'left');
        $this->db->join('patients', 'patients.id = cases.patient_id', 'left');            
        $this->db->join('users', 'users.username = billing.user', 'left');
        $this->db->join('billing_payments', 'billing_payments.billing_id = billing.id', 'left');
        $this->db->join('billing_items', 'billing_items.billing_id = billing.id', 'left');
        $this->db->join('services', 'services.title = billing_items.service', 'left');
        $this->db->select('
            billing.id,
            billing.remarks,
            billing.status,
            billing.created_at,
            billing.updated_at,
            users.name as user,
            users.username,
            cases.id as case_id,
            cases.title as case_title,
            patients.id as patient_id,
            CONCAT(patients.lastname, ", ", patients.fullname) as patient_name,
            SUM(billing_payments.amount) as payments,
            SUM((services.amount - billing_items.discount)*billing_items.qty) as payables
        ');

        if(is_int($case_id)) {
            $this->db->where('billing.case_id', $case_id);           
        }

        if(is_int($patient_id)) {
            $this->db->where('patients.id', $patient_id);
        }

        if(is_int($status)) {
            $this->db->where('billing.status', $status);
        }

        $this->db->group_by('billing.id');
        $this->db->where('billing.is_deleted', 0);

        $query = $this->db->get("billing");

        log_message('error', $this->db->last_query());
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }

        return false;

}

我使用Last Query和Log Helpers保存了Last Query,我的实例的查询似乎是

 SELECT `billing`.`id`, `billing`.`remarks`, `billing`.`status`, `billing`.`created_at`, `billing`.`updated_at`, `users`.`name` as `user`, `users`.`username`, `cases`.`id` as `case_id`, `cases`.`title` as `case_title`, `patients`.`id` as `patient_id`, CONCAT(patients.lastname, ", ", patients.fullname) as patient_name, SUM(billing_payments.amount) as payments, SUM((services.amount - billing_items.discount)*billing_items.qty) as payables
FROM `billing`
LEFT JOIN `cases` ON `cases`.`id` = `billing`.`case_id`
LEFT JOIN `patients` ON `patients`.`id` = `cases`.`patient_id`
LEFT JOIN `users` ON `users`.`username` = `billing`.`user`
LEFT JOIN `billing_payments` ON `billing_payments`.`billing_id` = `billing`.`id`
LEFT JOIN `billing_items` ON `billing_items`.`billing_id` = `billing`.`id`
LEFT JOIN `services` ON `services`.`title` = `billing_items`.`service`
WHERE `billing`.`is_deleted` =0

为了更好地指导您解决当前问题,这是相关表格的架构:

enter image description here

这是整个数据库的架构。

enter image description here

1 个答案:

答案 0 :(得分:0)

使用group by语句。因为mysql聚合函数适用于group by。如果您想要针对患者的结算金额,请使用以下查询

SELECT 
    `billing`.`id`, 
    `billing`.`remarks`, 
    `billing`.`status`, 
    `billing`.`created_at`, 
    `billing`.`updated_at`, `
    users`.`name` as `user`, 
    `users`.`username`, 
    `cases`.`id` as `case_id`, 
    `cases`.`title` as `case_title`, 
    `patients`.`id` as `patient_id`, 
    CONCAT(patients.lastname, ", ", patients.fullname) as patient_name, 
    SUM(billing_payments.amount) as payments, 
    SUM((services.amount - billing_items.discount)*billing_items.qty) as payables
FROM `billing`
    LEFT JOIN `cases` ON `cases`.`id` = `billing`.`case_id`
    LEFT JOIN `patients` ON `patients`.`id` = `cases`.`patient_id`
    LEFT JOIN `users` ON `users`.`username` = `billing`.`user`
    LEFT JOIN `billing_payments` ON `billing_payments`.`billing_id` = `billing`.`id`
    LEFT JOIN `billing_items` ON `billing_items`.`billing_id` = `billing`.`id`
    LEFT JOIN `services` ON `services`.`title` = `billing_items`.`service`
WHERE `billing`.`is_deleted` =0
GROUP BY patients.id