使用内连接查询表?

时间:2017-10-19 11:50:03

标签: sql inner-join

我们正在尝试查找同一分行同时拥有贷款和帐户的客户的姓名。我们应该在这里使用inner join吗?到目前为止,我们只写了;

select DISTINCT customer.name
FROM Customer, Has_Loan, Branch, Has_Account 
WHERE 

enter image description here

我们已经尝试了一些不同的东西而没有进一步,所以感谢任何帮助或提示:)

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

SELECT DISTINCT Customer.Name
FROM Customer
    INNER JOIN Has_Account ON Customer.Ssn = Has_Account.Assn
    INNER JOIN Account ON Account.AccountNo = Has_Account.ANo
    INNER JOIN Has_Loan ON Customer.Ssn = Has_Loan.Lssn
    INNER JOIN Loan ON Loan.LoanNo = Has_Loan.LNo
WHERE Loan.BranchID = Account.BranchID 

答案 1 :(得分:1)

select c.name
from customer c
join has_loan hl on hl.lssn = c.ssn
join loan l on hl.lno = l.loanno
join has_account ha on ha.assn = c.ssn
join account a on ha.ano = a.accountno
join branch b on b.branchid = a.branchid
where l.branchid  = a.branchid
group by c.name;