Mysql
 sql >> Datenbank >  >> RDS >> Mysql

MySQL-Abfragen an ein mehrdimensionales PHP-Array

Versuchen Sie, alle Zeilen bei einem JOIN abzufragen und dann iterieren, um mehrdimensional zu rendern:

$query = "SELECT *, materials.id AS material_id FROM job_truck JOIN job_truck_material ON job_truck_material.job_truck = job_truck.id JOIN materials ON job_truck_material.material = job_truck_material.id";
$jobs = array();

foreach ($conn->query($query ) as $row) {
    $job_id = $row['jobid'];
    $truck_id = $row['truck_id'];
    if(empty($jobs[$job_id])) {
        $jobs[$job_id] = array();
    }
    if(empty($jobs[$job_id][$truck_id])) {
        $jobs[$job_id][$truck_id] = array();
    }
    $jobs[$job_id][$truck_id][$row['material_id']] = $row['material'];
}