我有陈述A和陈述B.
语句A返回5条记录,语句B返回1条记录。
声明A
SELECT DISTINCT
Outlet.cCode,
Employee.cEmployeeNumber,
FROM Outlet
INNER JOIN Employee ON EmployeeOutlet.iEmployee = Employee.iID
WHERE cCOde = 123
声明B
Select OutletCode, RSMcode, ASMcode, FMcode from Employee_Hierarchy
Where OutletCode = 123
现在,只有3个来自Statement A的记录必须与Employee一起返回.cmployeeNumber等于RSMcode,ASMcode或FMcode ......
现在如果我在其中添加以下JOIN将只返回1条记录,我如何迎合仍然需要的其他2条呢?
SELECT DISTINCT
Outlet.cCode,
Employee.cEmployeeNumber,
FROM Outlet
INNER JOIN Employee ON EmployeeOutlet.iEmployee = Employee.iID
INNER JOIN Employee_Hierarchy as EH ON EH.RSMcode = Employee.cEmployeeNumber and EH.OutletCode = Outlet.cCode
WHERE Outlet.cCode = 123
当我为每个RSMcode和ASMcode和FMcode添加一个JOIN时,它什么都不返回.....
答案 0 :(得分:1)
这是怎么回事?
SELECT DISTINCT
Outlet.cCode,
Employee.cEmployeeNumber,
FROM Outlet
INNER JOIN Employee ON EmployeeOutlet.iEmployee = Employee.iID
INNER JOIN Employee_Hierarchy as EH ON EH.OutletCode = Outlet.cCode AND (EH.RSMcode = Employee.cEmployeeNumber OR EH.ASMcode = Employee.cEmployeeNumber OR EH.FMcode = Employee.cEmployeeNumber)
WHERE Outlet.cCode = 123
答案 1 :(得分:0)
只是一个猜测
SELECT DISTINCT
Outlet.cCode,
Employee.cEmployeeNumber,
FROM Outlet
INNER JOIN Employee ON EmployeeOutlet.iEmployee = Employee.iID
INNER JOIN Employee_Hierarchy as EH ON EH.RSMcode = Employee.cEmployeeNumber and EH.OutletCode = Outlet.cCode
and EH.OutletCode = 123
答案 2 :(得分:0)
INNER JOIN Employee_Hierarchy as EH ON EH.RSMcode = Employee.cEmployeeNumber and outletCode = Outlet.cCode
将此段代码更改为
INNER JOIN Employee_Hierarchy as EH ON EH.OutletCode = Outlet.cCode
WHERE
Employee.cEmployeeNumber=EH.RSMcode OR
Employee.cEmployeeNumber=EH.ASMcode OR
Employee.cEmployeeNumber=EH.FMcode