在提供的表中选择当日的放置贷款和已清算贷款的金额

时间:2019-06-14 06:28:19

标签: php mysql codeigniter

我正在构建一个包含用户贷款信息的应用程序,但是问题是如何选择当天的已放置贷款和已清算贷款的数量;例如,如果用户在7:00 AM放款并在同一天的13:00 PM进行清算,我该如何在同一天清算并清算这笔贷款?

我的示例表是:

k[0].count

例如,ID为2的用户在19/6/14放贷2000.00并清算了3000.00的贷款,也就是说,放到19/6/14并清算19/6 /的贷款金额用户2的14是2000.00;剩余的1000.00为12/6/12。

如何选择此数据?

3 个答案:

答案 0 :(得分:0)

减去在日期之前清算的贷款。然后从清除的日期中减去该值。

    $this->db->select('(SUM(placed_loan) - SUM(cleared_loan))');
    $this->db->where('user_id', $user_id);
    $this->db->where('plcd_date <', $date);
    $subquery = $this->db->get_compiled_select('yourtablename');
    $this->db->select("placed_loan, (cleared_loan - ($subquery)) as cleared_to_date");
    $this->db->where('user_id', $user_id);
    $this->db->where('plcd_date', $date);
    $q = $this->db->get('yourtablename');
    if ($q->num_rows() == 0) {
        die('no rows');
    }
    $res = $q->row();
    echo "placed: $res->placed_loan"; // 2000
    echo "cleared_to_date: $res->cleared_to_date"; // 2000
    exit;

答案 1 :(得分:0)

尝试此查询

SELECT A.id as id, A.user_id as userid , A.plcd_date as date , SUM(A.placed_loan) as loan_amt , SUM(B.cleared_loan) clr_amt FROM `user_loan` as A , `user_loan` as B WHERE A.plcd_date = B.cld_date GROUP BY A.plcd_date, A.user_id
  • 结果1
  • 数据2

答案 2 :(得分:0)

感谢所有参与解决我的问题的人:

 $data['financial_year_start'] = '2019-06-10';
        $data['recon_date'] = '2019-06-12';
        //$user_id = 1;
        $this->db->select('(SELECT SUM(placed_loan) FROM customers_loans WHERE loan_added_on >="'.$data['financial_year_start'].'" AND loan_added_on <"'.$data['recon_date'].'") AS plcd_before', FALSE);
        $this->db->select('(SELECT SUM(placed_loan) FROM customers_loans WHERE loan_added_on ="'.$data['recon_date'].'") AS plcd_today', FALSE);
        $this->db->select('(SELECT SUM(cleared_loan) FROM customers_loans WHERE loan_added_on >="'.$data['financial_year_start'].'" AND loan_added_on <"'.$data['recon_date'].'") AS cld_before', FALSE);
        $this->db->select('(SELECT SUM(cleared_loan) FROM customers_loans WHERE loan_added_on ="'.$data['recon_date'].'") AS cld_today', FALSE);
        $q = $this->db->get()->result_array();;


       $data['plcd_bbf'] = $q[0]['plcd_before'] - $q[0]['cld_before'];
       $data['plc_cld_today'] = $q[0]['cld_today'] - $data['plcd_bbf'];
       echo $data['plc_cld_today']; // This is the amount of loan placed and cleared on the same day