简体   繁体   中英

Merge data from multiple tables into an array with multiple values

In the following example the record data is loaded on the client side in a grid.

table my_cars

id   car_name
1      Ford
2      BMW

table cars_detail

id_detail  id_car  country  car_detail
 1           1       USA        red
 2           1       USA        blue
 3           2       GER        green

If the user selects a row the record data is loaded in an update form.

The data must be received on the client side in JSON with the following format:

First record

    car_name : "Ford"  

    my_cars: Array(2)
    0: {id_detail: "1", country: "USA", car_detail: "red"}
    1: {id_detail: "2", country: "USA", car_detail: "blue"}

Second record

    car_name : "BMW"      

    my_cars: Array()
    0: {id_detail: "1", country: "GER", car_detail: "green"}

I was struggling to get my_cars Array values in the desired format.

If I just needed to get the data from the cars_detail table I would do the following way which works correctly:

$query = "SELECT id_detail, country, car_detail
          FROM cars_detail";

    $statement = $conexion->query($query);

    while($row = $statement->fetch_assoc()) {
        $my_cars[] = array(
              'id_car'=> $row['id_car'],
              'country'=> $row['country'],
              'car_detail'=> $row['car_detail']
            );
    };

However I need to get the data from the two tables for each record.

In one of my attempts I'm trying with GROUP_CONCAT in my query and then, somehow, use explode to create the correct array.

$query = "SELECT mc.car_name
      GROUP_CONCAT(DISTINCT CONCAT( 'id_detail', ':', cd.id_detail,  'country', ':', cd.country, 'car_detail', ':', cd.car_detail)  SEPARATOR ',')  AS all_detail_values,
      FROM my_cars mc          
      LEFT JOIN cars_detail cd ON mc.id_car = cd.id_car
      LIMIT ?,?";

if ($statement = $conexion->prepare($query)){

  $statement->bind_param("ii", $start, $limit);
  $statement->execute();

  statement->bind_result($car_name, $all_detail_values);

    while($statement->fetch()){

       //This solution does not work correctly
        $values = explode(",", $all_detail_values);
         foreach($values as $value){
            list($key,$val) = explode(" : ", $value);
            $my_cars[][$key] = $val;
         }


        $output[] = array(
            'car_name'=>$car_name,
            'my_cars'=>$my_cars
        );
    };


echo json_encode(array(
    "records_values" => $output
);

What is the best way to solve this type of case?

EDITED

Sorry if I did not explain this in the best way. Here's a http://sqlfiddle.com/#!9/456203/11 :

my_cars must be in array format

I don't understand why you need to GROUP_CONCAT it. What's wrong with simply listing the column?

SELECT mc.car_name, cd.id_detail, cd.country, cd.car_detail
FROM my_cars mc          
LEFT JOIN cars_detail cd ON mc.id_car = cd.id_car
LIMIT ?,?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM