;WITH tblEmployee AS (
SELECT * FROM (VALUES
(1, 'Tom', 'Male', 4000, 1),
(2, 'Pam', 'Female', 3000, 3),
(3, 'John', 'Male', 3500, 1),
(4, 'Sam', 'Male', 4500, 2),
(5, 'Todd', 'Male', 2800, 2),
(6, 'Ben', 'Male', 7000, 1),
(7, 'Sara', 'Female', 4800, 3),
(8, 'Valarie', 'Female', 5500, 1),
(9, 'James', 'Male', 6500, NULL),
(10, 'Russell', 'Male', 8800, NULL)) AS E(ID, Name, Gender, Salary, DepartmentId)
),
tblDepartment AS (
SELECT * FROM (VALUES
(1, 'IT', 'London', 'Rick'),
(2, 'Payroll', 'Delhi', 'Ron'),
(3, 'HR', 'New York', 'Christie'),
(4, 'Other Department', 'Sydney', 'Cindrella')) AS D(Id, DepartmentName, Location, DepartmentHead)
)
左表是员工表,右表是部门表。
我正在学习在线教程,但对讲师提供的解释感到困惑:
如果我在上面的两个表上运行这个查询:
Select E.Name, E.Gender, E.Salary, D.DepartmentName
from tblEmployee as E
Left Join tblDepartment as D
On tblEmployee.departmentID = tblDepartment.Id
Where tblEmployee.departmentID IS Null;
我对上面 SQL 代码中最后一行的作用有些困惑。当你说:
Where tblEmployee.departmentID IS Null
上面这行代码是在运行这行代码后形成的新表上执行的吗:
Select E.Name, E.Gender, E.Salary, D.DepartmentName
from tblEmployee as E
Left Join tblDepartment as D
On tblEmployee.departmentID = tblDepartment.Id
所以基本上它会检查形成的新表:只有部门 ID 为 NULL 的地方才会显示那些行。我只是想澄清这一行:Where tblEmployee.departmentID IS Null
is being running after the new table is formed correct?
最佳答案
关于SQL 智能连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35824750/