谁是在同一分支中同时拥有账户和贷款的客户?

时间:2015-11-14 23:48:30

标签: mysql sql

我有这些表格: http://imgur.com/ylyXYZP

我想知道:在同一个分支中同时拥有帐户和贷款的客户是谁?

我尝试了两段代码,如下所示:

select distinct depositor.customer_name, account.branch_name
from (borrower inner join loan on borrower.loan_number=loan.loan_number) 
inner join 
( depositor inner join account on depositor.account_number=account.account_number ) 
on (account.branch_name=loan.branch_name and borrower.customer_name=depostior.customer_name);

和此:

select account.branch_name, depositor.customer branch_name
from account, depostior
where depositor.customer_name, account.branch_name
in ( borrower inner join loan on borrower.loan_number=loan.loan_number );

在最新版本中,我尝试过使用嵌套查询,但没有成功。

那我怎么解决这个问题呢? 感谢

2 个答案:

答案 0 :(得分:1)

select distinct d.customer_name, a.branch_name
from depositor d,  account a, borrower b, loan l
where 
d.account_number = a.account_number
and d.customer_name = b.customer_name
and b.loan_number = l.loan_number
and l.branch_name = a.branch_name

以INNER JOIN语法重写

SELECT DISTINCT d.customer_name, a.branch_name
FROM depositor d
INNER JOIN account a ON d.account_number = a.account_number
INNER JOIN borrower b ON d.customer_name = b.customer_name
INNER JOIN loan l ON b.loan_number = l.loan_number
       AND l.branch_name = a.branch_name

答案 1 :(得分:-1)

以INNER JOIN语法重写

SELECT DISTINCT
        d.customer_name, a.branch_name
    FROM
        depositor d
            INNER JOIN
        account a ON d.account_number = a.account_number
            INNER JOIN
        borrower b ON d.customer_name = b.customer_name
            INNER JOIN
        loan l ON b.loan_number = l.loan_number
            AND l.branch_name = a.branch_name