PHP& MYSQL加入两个表

时间:2015-09-11 02:23:59

标签: php join mysqli

我有两张表machine_infoparts_repalced

machine_info

id | machine_name | desc | date_added | parts_ref
1  | printer      | desc1| 09/09/15   | 7

parts_replaced

id | machine_ref_id | partno | qty
1  | 7              | KFRy23 | 2
2  | 7              | RY7RA1 | 4

现在我希望它加入并显示如下

id | machine_name | desc | date_added | partno | qty
1  | printer      | desc1| 09/09/15   | KFRy23 | 2
                                      | RY7RA1 | 4

这是我到目前为止所做的......

$result = $db->query("SELECT * FROM machine_info LEFT OUTER JOIN parts_replaced ON machine_info.id = parts_replaced.machine_ref_id");

$data = "";
while($r = $result->fetch_object()) {
    if ($data != "") {$data .= ",";}
    $data .= '{"id":"'  . $r->id . '",';
    $data .= '"machine_name":"'   . $r->machine_name. '",';
    $data .= '"desc":"'   . $r->desc. '",';
    $data .= '"date_added":"'   . $r->date_added. '",';
    $data .= '"partno":"'   . $r->partno. '",';
    $data .= '"qty":"'. $r->qty. '"}'; 
    }
    $result->free();
$data ='{"records":['.$data.']}';
$db->close();
echo($data);

结果的问题是它给我一个重复的结果

任何帮助将不胜感激。提前谢谢..

0 个答案:

没有答案