how to select n record child on every single parent record in relation data in mysql
suppose i have two table one is parent and second is child. parent can have n child and i have to pull record data with child where parent => id is local key and child => parent_id is foreign key. how to do that with efficient way.
parent_table
+-------------------+
| id | name |
+-------------------+
| 1 | p1 |
| 2 | p2 |
| 3 | p3 |
| 4 | p4 |
| 5 | p5 |
| 6 | p6 |
+-------------------+
child_table
+------------------------------+
| id | parent_id | name |
+------------------------------+
| 1 | 1 | c1 |
| 2 | 1 | c2 |
| 3 | 2 | c3 |
| 4 | 3 | c4 |
| 5 | 3 | c5 |
| 6 | 3 | c6 |
| 7 | 5 | c7 |
| 8 | 6 | c8 |
| 9 | 6 | c9 |
+------------------------------+
result be like
Array
(
[0] => Array
(
[id] => 1
[name] => p1
[children] => Array
(
[0] => Array
(
[id] => 1
[parent_id] => 1
[name] => c1
)
[1] => Array
(
[id] => 2
[parent_id] => 1
[name] => c2
)
)
)
[0] => Array
(
[id] => 2
[name] => p2
[children] => Array
(
[0] => Array
(
[id] => 1
[parent_id] => 2
[name] => c3
)
)
)
)
i know it can be done with for loop (i work with php)