1

我有声明 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

现在只需要返回 Statement A 中的 3 条记录,其中 Employee.cEmployeeNumber 等于 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 时,它什么也不返回.....

4

3 回答 3

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
于 2012-11-23T09:19:28.097 回答
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 
于 2012-11-23T08:51:54.163 回答
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
于 2012-11-23T08:43:22.663 回答