MySQL获取每个数据库行中的2个

时间:2015-10-14 12:02:03

标签: php mysql sql select left-join

我无法弄明白为什么,但是我的代码会在每个数据库行中生成2个。

这是我的SELECT语句

//select all data
$query = "SELECT e.employeeid, e.name, e.title, e.phone, s.name as supplier_name, e.created 
            FROM employeestest e 
                LEFT JOIN supplierstest s 
                    ON s.supplierid 
            ORDER BY e.employeeid DESC
            LIMIT :from_record_num, :records_per_page";

$stmt = $con->prepare($query);
$stmt->bindParam(":from_record_num", $from_record_num, PDO::PARAM_INT);
$stmt->bindParam(":records_per_page", $records_per_page, PDO::PARAM_INT);
$stmt->execute();

//number of rows returned
$num = $stmt->rowCount();

这是我填充表格的地方(在此之前有标题,这就是为什么你没有起始表标签)

// retrieve table contents
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){

    extract($row);

    //creating new table row per record
    echo "<tr>";
        echo "<td class='text-align-center'><input type='checkbox' name='item[]' class='checkboxes' value='{$employeeid}' /></td>";
        echo "<td>{$name}</td>";
        echo "<td>{$title}</td>";
        echo "<td>{$phone}</td>";
        echo "<td>{$supplier_name}</td>";
        echo "<td>{$created}</td>";
        echo "<td>";

            // update record
            echo "<a href='update_product.php?employeeid={$employeeid}' class='btn btn-info margin-right-1em'>";
                echo "<span class='glyphicon glyphicon-edit'></span> Rediger";
            echo "</a>";

            // delete record
            echo "<a delete-employeeid='{$employeeid}' delete-file='delete_product.php' class='btn btn-danger delete-object'>";
                echo "<span class='glyphicon glyphicon-remove'></span> Slet";
            echo "</a>";

        echo "</td>";
    echo "</tr>";
}
//end table<br>
echo "</table>";

如果需要更多代码,请告诉我们!

2 个答案:

答案 0 :(得分:5)

您的join条件错误。你需要一个条件。像这样:

        FROM employeestest e LEFT JOIN
             supplierstest s 
             ON s.supplierid = e.supplierid
-----------------------------^

或者用于链接两个表的适当列。

答案 1 :(得分:1)

use like this: 

$query = "SELECT e.employeeid, e.name, e.title, e.phone, s.name as supplier_name, e.created 
        FROM employeestest e 
            LEFT JOIN supplierstest s 
                ON s.supplierid = e.supplierid  
        ORDER BY e.employeeid DESC
        LIMIT :from_record_num, :records_per_page";

- e.supplierid或e.suppliedcoumnname无论是什么。