表别名 - 引用同一表的多个外键

时间:2016-01-27 17:18:29

标签: mysqli prepared-statement inner-join alias

我无法弄清楚如何为表使用别名(我认为别名是解决我问题的正确方法)。我有表Su​​bContract有3个字段:SubcontractID,DivisionManager和ServiceManager。这两个字段都通过EmployeeID与同一个表Employee相关。表Employee包括EmployeeID和FirstName字段。我无法弄清楚如何同时显示DivisionManager和ServiceManager的FirstName。我使用这种方法但仍然没有工作:

$query = "SELECT SubContract.SubContractID, SubContract.DivisionManagerEmployeeID, Employee.FirstName, SubContract.ServiceManagerEmployeeID, Employee.FirstName FROM SubContract 

INNER JOIN Employee  Employee1 ON Employee1.EmployeeID = SubContract.DivisionManagerEmployeeID
INNER JOIN Employee  Employee2 ON Employee2.EmployeeID = SubContract.ServiceManagerEmployeeID";

$stmt = $db->prepare($query);
$stmt->execute();
$stmt->store_result();
$numrows = $stmt->num_rows;
$stmt>bind_result($SubContractID,$DivisionManagerEmployeeID,$DivisionMangerFirstName, $ServiceManagerEmployeeID,$ServiceMangerFirstName);

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

我只想弄清楚:

SELECT SubContract.SubContractID, SubContract.DivisionManagerEmployeeID,DivisionManager.FirstName,  SubContract.ServiceManagerEmployeeID, ServiceManager.FirstName 

FROM SubContract

INNER JOIN Employee ServiceManager ON ServiceManager.EmployeeID = SubContract.ServiceManagerEmployeeID
INNER JOIN Employee DivisionManager ON DivisionManager.EmployeeID = SubContract.DivisionManagerEmployeeID