-5

银行数据库架构:

account    (account_number, branch_name, balance) 
branch     (branch_name, branch_city, assets) 
customer   (customer_name customer_street, customer_city) 
loan       (loan_number, branch_name, amount) 
deposito   (customer_name, account_number) 
borrower   (customer_name, loan_number) 

我怎么知道借款人的帐号????

4

2 回答 2

5

试试这个..(根据你的评论)

SELECT c.customer_name,
  a.account_number
FROM customer c
  INNER JOIN depositor d
    ON c.customer_name = d.customer_name
  INNER JOIN borrower b
    ON d.customer_name = b.customer_name
  INNER JOIN account a
    ON a.account_number = d.account_number

演示

更新:

SELECT b.customer_name,
  d.account_number
FROM borrower b
  INNER JOIN depositor d
    ON d.customer_name = b.customer_name
  INNER JOIN account a
ON a.account_number = d.account_number

演示

于 2013-03-13T13:41:57.167 回答
4

根据您的评论,看起来customers与表格有关。如果是这样,那么您可以使用:accountsdepositor

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表只能加入accounton branch_number,这个分支号将与多个帐户相关联。您应该有一个列来标识borrowereach loan,因为缺少此列,您目前无法找到每个借款人的帐号。

于 2013-03-13T13:15:35.173 回答