选择3个表的语句

时间:2013-02-08 02:59:13

标签: php sql-server select join

所以我有一个php程序,我应该能够打印变量

表1 dbo.FA_PC(表名)
FAID(PK)
PCID(FK)
用户ID(FK)

表2 dbo.users(表名)
用户ID(PK)
BranchID(FK)
Employeename

表3 dbo.Branch(表名)
BranchID(PK)
Branchname

<?php
$faid=$_POST['faid'];
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 * from dbo.users inner join dbo.FA_PC on dbo.userd.UserID = dbo.FA_PC.UseriD WHERE FAID = $faidf";    
   $rs = $conn->Execute($sql_exp);  

    echo "<tr><td>PC Number:".$rs->Fields("Employeename")."</td>";
?>  

我想要做的是当选择FAID时可以发布所选FAID的Branchname

1 个答案:

答案 0 :(得分:0)

SELECT  a.*, b.*, c.*  // select columns you want
FROM    users a
        INNER JOIN FA_PC b
            ON a.userID = b.UserID
        INNER JOIN Branch c
            ON a.BranchID = c.BranchID
WHERE   b.FAID = 'valueHere'

要充分了解联接知识,请访问以下链接: