我浏览了一篇关于CROSS APPLY
和OUTER APPLY
SQL Server下表用于说明两者。
员工表:
EmployeeID FirstName LastName DepartmentID
1 Orlando Gee 1
2 Keith Harris 2
3 Donna Carreras 3
4 Janet Gates 3
部门表:
DepartmentID Name
1 Engineering
2 Administration
3 Sales
4 Marketing
5 Finance
我知道这OUTER APPLY
类似于LEFT OUTER JOIN.
但是当我OUTER APPLY
在下表之间应用时,
select * from Department e
outer apply
Employee d
where d.DepartmentID = e.DepartmentID
我得到以下结果(与INNER JOIN
结果相同)
DepartmentID Name EmployeeID FirstName LastName DepartmentID
1 Engineering 1 Orlando Gee 1
2 Administration 2 Keith Harris 2
3 Sales 3 Donna Carreras 3
3 Sales 4 Janet Gates 3
当我OUTER APPLY
在如下表之间应用时(right table
作为子查询)。
select * from Department e
outer apply
(
select * from
Employee d
where d.DepartmentID = e.DepartmentID
)a
我得到以下结果(与LEFT OUTER JOIN
结果相同)
DepartmentID Name EmployeeID FirstName LastName DepartmentID
1 Engineering 1 Orlando Gee 1
2 Administration 2 Keith Harris 2
3 Sales 3 Donna Carreras 3
3 Sales 4 Janet Gates 3
4 Marketing NULL NULL NULL NULL
5 Finance NULL NULL NULL NULL
有人可以解释为什么这两个查询给出的不同outputs
吗?