0

所以我有 5 个表,其中它与外键互连

5桌

这是表 3 的示例输出

表3输出

我想在表 3 中做的是提取用户 ID 为 10 的用户的 SubdeptID,但在这种情况下它有 2 个用户 ID10,所以它打印两者。我要打印的只是带有后一个 TransferID 的那个。我的选择语句是这样的

$sql_exp = "SELECT  a.UserID, b.Employeename, c.TransferID, e.Department
FROM    dbo.FA_Laptop 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.SudDeptID = d.SubDeptID
        INNER JOIN dbo.departments e
            ON d.DeptID = e.DeptID
WHERE  a.FAID = '$faidf' ORDER by c.TransferID DESC LIMIT 1"; 

我的 php 代码是

$rs = $conn->Execute($sql_exp);      
    if ($rs->EOF) {
    echo "<tr><td>Please check for the Employee Name or the Department</td>";
} else {
    while (!$rs->EOF){ 
    echo "<tr><td>".$rs->Fields("Department")." / ".$rs->Fields("EmployeeName")."</td>";
    $rs->movenext();
    }
       $rs->Close();  
}   

我在“限制”查询中有错误。

4

1 回答 1

1

MSSQL 没有 LIMIT 关键字。

使用TOP代替 LIMIT。

$sql_exp = "SELECT TOP 1 a.UserID, b.Employeename, c.TransferID, e.Department
FROM    dbo.FA_Laptop 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.SudDeptID = d.SubDeptID
        INNER JOIN dbo.departments e
            ON d.DeptID = e.DeptID
WHERE  a.FAID = '$faidf' ORDER by c.TransferID DESC"; 
于 2013-02-20T03:48:42.177 回答