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