根据您的评论,看起来customers
与表格有关。如果是这样,那么您可以使用:accounts
depositor
select c.customer_name,
a.account_number,
a.balance
from customer c
inner join depositor d
on c.customer_name = d.customer_name
inner join account a
on d.account_number = a.account_number
请参阅带有演示的 SQL Fiddle
如果您想知道借款人的帐号,那么您将在borrower
表中添加一个 JOIN:
select c.customer_name,
a.account_number,
a.balance
from customer c
inner join depositor d
on c.customer_name = d.customer_name
inner join account a
on d.account_number = a.account_number
inner join borrower b
on c.customer_name = b.customer_name
您的数据库设计似乎存在一些问题。根据您当前的结构,如果您想知道borrowers
,那么您必须加入depositor
以获取帐号或loan
表格以将贷款与帐户相关联。
如果您加入loan
表格,查询将是:
select b.customer_name,
a.account_number
from borrowers b
inner join loan l
on b.loan_number = l.loan_number
inner join account a
on l.branch_number = a.branch_number
但问题是该loan
表只能加入account
on branch_number
,这个分支号将与多个帐户相关联。您应该有一个列来标识borrower
each loan
,因为缺少此列,您目前无法找到每个借款人的帐号。