MySQL 贷款、付款和客户

MySQL loans, payments and clients

我有 3 个表“客户”、“贷款”和“付款”

TABLE 客户:

+------------+-------------+
| id_client  | name        |
+------------+-------------+
| 1          | James       |
| 2          | Robert      |
| 3          | Michael     |
+------------+-------------+

TABLE 贷款:

+-------------+-------------+-------------+-------------+
| id_loan     | id_client   | date_loan   | amount_loan |
+-------------+-------------+-------------+-------------+
| 1           | 2           | 2020-07-01  | 3000000     |
| 2           | 3           | 2021-07-12  | 2000000     |
+-------------+-------------+-------------+-------------+

TABLE 付款:

+-------------+-------------+--------------+----------------+
| id_payment  | id_loan     | date_payment | amount_payment |
+-------------+-------------+--------------+----------------+
| 1           | 1           | 2020-08-03   | 50000          |
| 2           | 1           | 2020-09-03   | 50000          |
| 3           | 1           | 2020-10-03   | 50000          |
| 4           | 1           | 2020-11-03   | 50000          |
| 5           | 1           | 2020-12-03   | 50000          |
| 6           | 1           | 2021-01-03   | 50000          |
| 7           | 1           | 2021-02-03   | 50000          |
| 8           | 1           | 2021-03-03   | 50000          |
| 9           | 1           | 2021-04-03   | 50000          |
| 10          | 1           | 2021-05-03   | 50000          |
| 11          | 1           | 2021-06-03   | 50000          |
+-------------+-------------+--------------+----------------+

我要打印这个:

+-------------+-------------+-------------+-------------+-------------+
| id_loan     | client      | date_loan   | amount_loan | amount_left |
+-------------+-------------+-------------+-------------+-------------+
| 1           | Robert      | 2020-07-01  | 3000000     | 2450000     |
| 2           | Michael     | 2021-07-12  | 2000000     | 2000000     |
+-------------+-------------+-------------+-------------+-------------+

请注意,迈克尔有一笔贷款,但尚未付款。

我几乎完成了这个查询:

SELECT loans.id_loan,
   clients.name,
   loans.date_loan,
   loans.amount_loan,
   (loans.amount_loan-SUM(payments.amount_payment)) AS amount_left
FROM loans
LEFT JOIN clients ON loans.id_client = clients.id_client
LEFT JOIN payments ON loans.id_loan = payments.id_loan

但是迈克尔显然没有被列出,因为他还没有付款。

您只需添加一个 GROUP BY loans.id_loan,然后应该会出现缺少的行。另外,我建议将 clients 上的 JOIN 更改为 JOIN.

SELECT loans.id_loan,
   clients.name,
   loans.date_loan,
   loans.amount_loan,
   (loans.amount_loan-SUM(payments.amount_payment)) AS amount_left
FROM loans
JOIN clients ON loans.id_client = clients.id_client
LEFT JOIN payments ON loans.id_loan = payments.id_loan
GROUP BY loans.id_loan

演示:http://sqlfiddle.com/#!9/32ca4/18

使用子选择获取付款

SELECT loans.id_loan,
   clients.client_name,
   loans.date_loan,
   loans.amount_loan,
   (loans.amount_loan-IFNULL((SELECT SUM(payments.amount_payment) FROM payments WHERE id_loan      = loans.id_loan   ),0)) AS amount_left
FROM loans
LEFT JOIN clients ON loans.id_client = clients.id_client

example