简体   繁体   English

选择内连接4表

[英]select inner join 4 tables

so i want to get a specific data from table number 4 which is interconnected to the other 3 table the user will choose the $faid and will print the data needed 所以我想从表号4获得一个特定的数据,这个数据与另一个3表互连,用户将选择$ faid并打印所需的数据

table 1 (dbo.FAID) 表1(dbo.FAID)
FAID(pk) FAID(PK)
PCID(fk) PCID(FK)
UserID(fk) 用户名(FK)

table 2 (dbo.users) 表2(dbo.users)
UserID(PK) 用户ID(PK)
EmployeeName 员工姓名

table 3(dbo.SubDeptTransfer) 表3(dbo.SubDeptTransfer)
TransferID(pk) TransferID(PK)
UserID(fk) 用户名(FK)
SubDeptID(fk) SubDeptID(FK)

table 4 (SubDept) 表4(SubDept)
SubDeptID(PK) SubDeptID(PK)
DeptID(fk) DEPTID(FK)

table 5(department) 表5(部门)
DeptID(PK) DEPTID(PK)
Department

<?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();   

?>

all i could get is "Invalid column name 'SubDeptID" which is im certain that the column name is correct though i think i mess up with my select statement 所有我能得到的是“无效的列名'SubDeptID”,我确定列名是正确的,虽然我认为我搞砸了我的选择语句

FAID->users->subdepttransfer->subdept->department FAID->用户 - > subdepttransfer-> subdept->部门

is there any conflict of how many inner joins has been made or it cannot execute more than 3 tables? 有多少内连接已经发生冲突,或者它不能执行超过3个表? if yes is there any way to connect the 5 tables? 如果是,有没有办法连接5个表?

There is no restriction AFAIK, at least 5 tables should not be a problem. AFAIK没有限制,至少5个表应该不成问题。 In your case there is a typo in the SQL statement. 在您的情况下,SQL语句中存在拼写错误。 You use alias a, but i think you meant to use c (I have also fixed the DeptID - your next error after you fix the SubDeptID). 你使用别名a,但我认为你打算使用c(我还修复了DeptID - 你修复SubDeptID后的下一个错误)。 Try this statement 试试这个说法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM