i have a db table category_path it is in parent child relationship and its look like this
-----------------------------
id | parent_id
------------------------------
1 | NULL
2 | 1
3 | 2
4 | 1
5 | 3
6 | 2
using this table i want to create a new table which will give me output like this. Where the table below shows distance for each id from parent 0 to that id by traversing through its parent.
----------------------------------
# |id | parent_id | distance
----------------------------------
1 | 1 | 1 | 0
2 | 1 | 2 | 1
3 | 1 | 3 | 2
4 | 1 | 4 | 1
5 | 1 | 5 | 3
6 | 1 | 6 | 2
7 | 2 | 2 | 0
8 | 2 | 3 | 1
9 | 2 | 5 | 2
10 | 2 | 6 | 1
11 | 3 | 3 | 0
12 | 3 | 5 | 1
13 | 4 | 4 | 0
14 | 5 | 5 | 0
15 | 6 | 6 | 0
如何通过数据库查询或编码做到这一点?