我有两张看起来像这样的桌子
用户
id
name
贷款
id
borrower_id
lender_id
amt
我需要选择所有贷款,在选择中匹配他们的名字,所以对于以下数据
用户
id, name
1, mark
2, james
贷款
id, lender_id, borrower_id, amt
1, 1, 2, 100
2, 2, 1, 150
3, NULL, 1, 500
4, NULL, 2, 300
NULL 是在贷方没有回应的情况下。输出应该是
loan_id, lender, borrower, amt
1, mark, James, 100
2, James, mark, 100
3, --, mark, 100
4, --, mark, 100
这是我试过的
Select
loans.amt As amt,
borrower.name As borrower,
lender.name As lender,
loans.id AS loan_id
From
(users borrower Join
loans On borrower.id = loans.borrower_id) Join
users lender On lender.id = loans.lender_id