所以我想从表号4中获取一个与另一个3表互连的特定数据 用户将选择$ faid并将打印所需的数据
表1(dbo.FAID)
FAID(PK)
PCID(FK)
用户ID(fk)
表2(dbo.users)
用户ID(PK)
员工姓名
表3(dbo.SubDeptTransfer)
TransferID(PK)
用户名(FK)
SubDeptID(fk)
表4(SubDept)
SubDeptID(PK)
DeptID(fk)
<?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-&GT;用户 - &GT; subdepttransfer-&GT; subdept-&GT;部门
是否有任何内部联接已经发生冲突或者执行不超过3个表的冲突?如果是,有没有办法连接5个表?
答案 0 :(得分: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