2013-02-11 119 views
0

,所以我想從互連到其他3臺 用戶會選擇$ FAID,將打印數據所需選擇內連接4臺

表1(DBO表4號特定數據.FAID)
FAID(PK)
PCID(FK)
用戶ID(FK)

表2(dbo.users)
用戶ID(PK)
EmployeeName

表3(dbo.SubDeptTransfer)
TransferID(PK)
用戶ID(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 = "[email protected]"; 
    $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」,這是IM肯定的是列名是正確的,雖然我認爲我亂用我的select語句

FAID->用戶 - > subdepttransfer-> subdept - >部門

有多少內部連接已經取得或不能執行超過3個表什麼衝突?如果是的話,有沒有辦法連接5個表格?

+0

有妳數據庫中創建你的表之間的關係 – freaky 2013-02-11 05:29:18

+0

是與他們的外鍵 – Yinks 2013-02-11 05:30:32

+0

沿着互連,因爲你使用的是SQL服務器,我建議你嘗試設計查詢編輯器你的sql服務器。這可以幫助您查詢 – freaky 2013-02-11 05:34:31

回答

2

沒有限制,據我所知,至少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 
+0

謝謝你所有的答覆,我能夠連接它與cha給出的答案。有幾個錯別字和恐慌謝謝你:) – Yinks 2013-02-11 05:45:31