0

所以我想从表 4 中获取特定数据,该表与其他 3 个表互连,用户将选择 $faid 并打印所需的数据

表 1 (dbo.FAID)
FAID(pk)
PCID(fk)
UserID(fk)

表 2 (dbo.users)
UserID(PK)
EmployeeName

表 3(dbo.SubDeptTransfer)
TransferID(pk)
UserID(fk)
SubDeptID(fk)

表 4 (SubDept)
SubDeptID(PK)
DeptID(fk)

表 5(部门)
DeptID(PK)
部门

<?php
$faidf=$_POST['faidf'];
ini_set("display_errors","on");
$conn = new COM("ADODB.Connection");
   try {
   $myServer = "WTCPHFILESRV\WTCPHINV";
   $myUser = "sa";
   $myPass = "P@ssw0rd";
   $myDB = "wtcphitinventory";   
   $connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
   $conn->open($connStr); 
         if (! $conn) {
            throw new Exception("Could not connect!");
        }
   }

   catch (Exception $e) {
      echo "Error (File:): ".$e->getMessage()."<br>";
   }
if (!$conn)
  {exit("Connection Failed: " . $conn);}
  echo "<center>";
   echo "<table border='0' width ='100%' style='margin-left:90px'><tr><th></th><th></th></tr>";
   $sql_exp = "SELECT  e.Department
FROM    dbo.FA_PC a
        INNER JOIN dbo.users b
        on a.UserID = b.UserID
        INNER JOIN dbo.SubDeptTransfer c
            ON a.UserID = c.UserID  
        INNER JOIN dbo.SubDept d
            ON a.SubDeptID = d.SubDeptID
        INNER JOIN dbo.department e
            ON a.DeptID = e.DeptID
WHERE   a.FAID = $faidf";    
   $rs = $conn->Execute($sql_exp);  

    echo "<tr><td>".$rs->Fields("Department")."</tr></td>";
       $rs->Close();   

?>

我所能得到的只是“无效的列名'SubDeptID”,尽管我认为我的选择语句搞砸了,但我确定列名是正确的

FAID->用户->子部门转移->子部门->部门

进行了多少内部连接或它不能执行超过 3 个表是否存在冲突?如果是,有什么方法可以连接 5 个表吗?

4

1 回答 1

2

AFAIK 没有限制,至少 5 个表应该不是问题。在您的情况下,SQL 语句中有错字。您使用别名 a,但我认为您打算使用 c(我还修复了 DeptID - 修复 SubDeptID 后的下一个错误)。试试这个说法

SELECT  e.Department
FROM    dbo.FA_PC a
        INNER JOIN dbo.users b
        on a.UserID = b.UserID
        INNER JOIN dbo.SubDeptTransfer c
            ON a.UserID = c.UserID  
        INNER JOIN dbo.SubDept d
            ON c.SubDeptID = d.SubDeptID
        INNER JOIN dbo.department e
            ON d.DeptID = e.DeptID
WHERE   a.FAID = $faidf
于 2013-02-11T05:35:39.163 回答