duannuan0074 2017-09-25 09:01
浏览 43

SQL Join SUM无法正常工作

I am having a problem producing a result with my query. I wanted to produce SUM of the result of rows from another table which is related to the primary table.

What I failed to output is the sum of payments, and the sum of payables.

  • PROBLEM #1 - I cannot produce a correct output of the Payments. The query seemed to produce an formula of numrows * sum_of_payments

  • PROBLEM #2 - I cannot produce an output of the total payables.

I am using Codeigniter 3 and this is my model.

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;

}

I saved the Last Query with the Last Query and Log Helpers, and it seemed that the query for my instance is

 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

To guide you more with my present problem, this is the schema of the related tables:

enter image description here

and this is the schema of the entire database.

enter image description here

  • 写回答

1条回答 默认 最新

  • dongtangjie0495 2017-09-25 09:58
    关注

    Use group by statement. because mysql aggregate functions work well with group by. if you want billing sum against patient then use following 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
    GROUP BY patients.id
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答