我写了2个查询,一个是子查询
SELECT DISTINCT pp.employeeid
FROM payment pp JOIN Employee e
ON e.employeeid = pp.employeeid WHERE pp.employeeid IN
(SELECT employeeid
FROM employee
WHERE branchid IN
(SELECT branchid
FROM branch
WHERE code IN
(SELECT code
FROM bank p
WHERE code = 15)));
GO
和其他内部联接查询
select DISTINCT pp.employeeid from payment pp inner join employee e
on e.employeeid=pp.employeeid
inner join branch b on b.branchid=e.branchid
inner join bank br on br.code=b.code
where br.code=15
这两个查询返回相同的数据,我想问哪一个更合适?哪一个更适合使用..
答案 0 :(得分:0)
加入VS子查询:
您可以使用查询来自不同表的数据并可能共享相同的结果,但两者之间存在许多差异。了解差异以及何时使用连接或子查询来获取数据将是您的成功。 =)
<强>连接:强> 用于组合来自两个或多个表的行,基于它们之间的相关列。Source
<强>子查询:强> 也称为内部查询或内部选择,而包含子查询的语句也称为外部查询或外部选择。 Source
我更喜欢Joins进行这种操作,更容易阅读:
select DISTINCT pp.employeeid
from payment pp inner join employee e on e.employeeid=pp.employeeid
inner join branch b on b.branchid=e.branchid
inner join bank br on br.code=b.code
where br.code=15