我有一些表格:
现在我想找到所有在Perryridge分行同时拥有账户和贷款的客户。
我写的SQL查询是
SELECT distinct customer_name
FROM borrower, loan
WHERE borrower.loan_number = loan.loan_number
AND branch_name = 'Perryridge'
AND (branch_name, customer_name )
IN (SELECT branch_name, customer_name
FROM depositor, account
WHERE depositor.account_number = account.account_number )
我想知道是否还有其他更简单的SQL查询可以完成相同的问题,例如使用TABLE UNION。
答案 0 :(得分:2)
try this updated script
SELECT DISTINCT
customer_name
FROM customer c
INNER JOIN depositor d
ON d.customer_name=c.customer_name
inner join accounts a on d.account_number = a.account_number
INNER JOIN borrower b
ON b.customer_name = c.customer_name
INNER JOIN loan l
ON l.loan_number = b.loan_number
WHERE l.branch_name = a.branch_name
AND l.branch_name = 'Perryridge'
答案 1 :(得分:0)
选择不同的客户名称
来自借款人,贷款
WHERE借款人。贷款编号=贷款。贷款编号
AND branch_name ='Perryridge'
AND(分支名称,客户名称)
IN(选择branch_name,customer_name
从存款人,帐户
在哪里depositor.account_number = account.account_number)