简体   繁体   中英

MySQL Selecting Joined Table as array of results

I need to write a mysql query that will select the parent data from the Task table, do a left join on the child SubTask table and append the child data to an array under the Task array like so...

Task

 id| details        | created
 ===============================
 1 | This is....    | 2017-08-19
 2 | This is....    | 2017-08-20
 3 | This is....    | 2017-08-21

Sub Task

 id  | task_id | details        | created
 ============================================
 123 | 1       | This is....    | 2017-08-19
 234 | 1       | This is....    | 2017-08-20
 345 | 2       | This is....    | 2017-08-21
 456 | 2       | This is....    | 2017-08-21
 567 | 3       | This is....    | 2017-08-21


 [
        [
            'id'=>1,
            'details'=>'This is from the task table',
            'subTasks'=>[
                [
                    'id'=>123,
                    'details'=>'This is from the sub task table',
                ],
                [
                    'id'=>234,
                    'details'=>'This is from the sub task table',
                ]
            ]
        ], 
        [
            'id'=>2,
            'details'=>'This is from the task table',
            'subTasks'=>[
                [
                    'id'=>345,
                    'details'=>'This is from the sub task table',
                ],
                [
                    'id'=>456,
                    'details'=>'This is from the sub task table',
                ]
            ]
        ]
        [
            'id'=>3,
            'details'=>'This is from the task table',
            'subTasks'=>[
                [
                    'id'=>567,
                    'details'=>'This is from the sub task table',
                ]
            ]
        ]
    ]

Here is my query:

 $sql = "    SELECT    t.*,
                       (select st.* from SubTask) as SubTasks
             FROM      Task t
             LEFT JOIN SubTask st
                 ON       st.task_id = t.id
             ";

I know this query isn't right, Im just not sure how to write it otherwise.

You don't need a select subquery, just:

SELECT t.id AS task_id, t.details AS task_details, st.id AS subtask_id, st.details AS subtask_details
FROM Task t
LEFT JOIN SubTask st ON st.task_id = t.id

Then your PHP code can collect all the subtask info into an array.

$tasks = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $task_id = $row['task_id'];
    if (!isset($tasks[$task_id])) {
        $tasks[$task_id] = ['id' => $task_id, 'details' => $row['task_details'], 'subtasks' => []];
    }
    if ($row['subtask_id'] !== null) {
        $tasks[$task_id]['subtasks'][] = ['id' => $row['subtask_id'], 'details' => $row['subtask_details']];
    }
}

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