mysql:从两个表中获取数据取决于第一个表的ID和json

时间:2019-01-28 12:11:18

标签: php mysql json join

我有2个表,我想从第一个表中获取数据取决于“ id”,而第二个表中的数据取决于与JSON中第一个表中的“ id”相同的“ gid”。

示例:

First Table: 
- - - - - - -
id  name
1   Aaron
2   Caleb
3   Albert
4   Ethan

Second Table:
- - - - - - -
id  gid image
1   1   http://.......image1.jpg
2   1   http://.......image2.jpg
3   2   http://.......image3.jpg
4   3   http://.......image4.jpg
5   3   http://.......image5.jpg
6   3   http://.......image6.jpg

当我请求id = 1时,我想要结果,像这样:

"names": [
    {

     "id": 1,

     "name": "Aaron"
          "images":[
             {
                "id" :1
                "url":http://.......image1.jpg
             },
             {
                "id" :2
                "url":http://.......image2.jpg
             }
       ]
   }
]

这是我的一些代码:

$SqlInfo = "Select * from tabel1 where id='$id'";

$obj = json_decode($_GET["x"], false);
$stmt = $con->prepare($SqlInfo);
$stmt->bind_param("ss", $obj->table, $obj->limit);
$stmt->execute();
$result = $stmt->get_result();
$outp = $result->fetch_all(MYSQLI_ASSOC);

echo json_encode(array(
    'status' => 'Ok',
    'name' => $outp
    ));

更具体地说,我的上面的代码为第一个表带来json格式,我想将第二个表的结果插入到与json(第一个表)相同的结果中。 谢谢...

2 个答案:

答案 0 :(得分:0)

首先,您需要第一个表的结果才能解码为Array。

$first_table = array(
    '0'=> ['id'=> 1, 'name' =>'Aaron' ],
    '1'=> ['id'=> 2, 'name' =>'Caleb' ],
    '2'=> ['id'=> 3, 'name' =>'Albert'],
    '3'=> ['id'=> 4, 'name' =>'Ethan' ]
    );

//create function to retrieve images by ID from Second Table 
function get_images($id)
{
$SqlInfo = "Select id, image as url from tabel2 where gid='$id'";
$stmt = $con->prepare($SqlInfo);
$stmt->execute();
$result = $stmt->get_result();
$outp = $result->fetch_all(MYSQLI_ASSOC);

    return $outp;
}   

// create a new object
$new_array=array();

//Loop first table
foreach ( $first_table as $key => $value){

$new_array[$value['name']]  = [
                                'name' => $value['name'],
                                'id' => $value['id'],
                                'images' => get_images($value['id']) // Get images from function
                               ];

}

$final = json_encode($new_array);

这未经测试,但是解决方案应该朝这个方向

答案 1 :(得分:0)

首先,您可以使用内部联接合并两个表的结果

$sql = "select 
           t2.gid,
           t2.id,
           t2.image,
           ti.name 
        from 
            second_table as t2
        join first_table as t1 on t2.gid = t1.id 
        where id='$id'           
       ";

$obj = json_decode($_GET["x"], false);
$stmt = $con->prepare($SqlInfo);
$stmt->bind_param("ss", $obj->table, $obj->limit);
$stmt->execute();
$result = $stmt->get_result();
$outp = $result->fetch_all(PDO::FETCH_ASSOC);
/*PDO::FETCH_ASSOC return result in associative array */
$arr = array();
if(!empty($outp)){

    $i =1;/* this is used so that first table data can only be feed to array once*/

    foreach($outp as $val){
        if($i ==1){
            $arr['id'] = $val['gid'];
            $arr['name'] = $val['name'];
        }
        $tmp = array();
        $tmp['id'] = $val['gid'];
        $tmp['url'] = $val['image'];
        $arr['images'][] = $tmp;
        $i++;
        /*increment $i so that we cannot enter $val['name'] repeatedly*/ 
    }
}
$json_arr = array('status' => 'Ok','name' => $arr);
echo json_encode($json_arr);