1
我需要编写一个mysql查询,它将从Task表中选择父数据,在子SubTask表上执行左连接并将子数据追加到Task数组下的数组像这样......MySQL选择连接表作为结果数组
任务
id| details | created
===============================
1 | This is.... | 2017-08-19
2 | This is.... | 2017-08-20
3 | This is.... | 2017-08-21
子课题负责人
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',
]
]
]
]
这里是我的查询:
$sql = " SELECT t.*,
(select st.* from SubTask) as SubTasks
FROM Task t
LEFT JOIN SubTask st
ON st.task_id = t.id
";
我知道这个查询是不正确的,我只是不知道如何写它,否则。
显示您的餐桌结构 –
这是非常困难的帮助您没有显示您的表与一些信息。 – jjoselon
添加了最基本的表详细信息 – LargeTuna