Я хотел бы получить остаток по кредиту клиента, когда он / она добавит или погасит ссуды Моя таблица:
-------------------------------------------------------------
loan_id | customer_id | placed_loan | cleared_loan | date |
-------------------------------------------------------------
1 | 2 | 2,000.00 | | 8/6/20|
------------------------------------------------------------
2 | 4 | 1,000.00 | | 9/6/20|
------------------------------------------------------------
3 | 2 | | 1,000.00 |10/6/20|
------------------------------------------------------------
4 | 2 | 60,000.00 | |12/6/20|
------------------------------------------------------------
Я бы хотел, чтобы результаты моего выбора были такими:
Customer id | Placed amount | Cleared amount | loan balance | Date |
-----------------------------------------------------------------------
2 | 2,000.00 | | 2,0000.00 | 8/6/20|
-----------------------------------------------------------------------
2 | | 1,000.000 | 1,0000.00 |10/6/20|
-----------------------------------------------------------------------
2 | 60,000.00 | | 61,000.00 |12/6/20|
-----------------------------------------------------------------------
Мои коды моделей:
$this->db->select('loan_id, CONCAT(SUM(placed_loan) - SUM(cleared_loan)) AS balance, CONCAT(t2_1.first_name, " ", t2_1.last_name) AS loan_added_by, CONCAT(t2_2.first_name, " ", t2_2.last_name) AS loan_cleared_by, CONCAT(t2_3.first_name, " ", t2_3.last_name) AS loan_updated_by, placed_loan, cleared_loan, loan_status, t2_4.customer_name, description, loan_added_on, loan_updated_on');
$this->db->join('users t2_1', 't2_1.id = customers_loans.loan_added_by', 'left');
$this->db->join('users t2_2', 't2_2.id = customers_loans.loan_cleared_by', 'left');
$this->db->join('users t2_3', 't2_3.id = customers_loans.loan_updated_by', 'left');
$this->db->join('customers t2_4', 't2_4.customer_id = customers_loans.customer_name', 'left');
$this->db->join('customers_loans_info t2_5', 't2_5.loan_info_id = customers_loans.loan_info', 'left');
$this->db->order_by('loan_id', 'desc');
$this->db->where('customers_loans.customer_name ="'.$data['c_id'].'" AND loan_status <="2" AND loan_added_on >="'. $data['financial_year_start'].'" AND loan_added_on <="'. $data['financial_year_end'].'"');
return $this->db->get('customers_loans')->result_array();
Как я могу получить столбец ссуды?