說我有包含的字段post_id
和parent_post_id
後表。我想用帖子的「深度」計數來返回帖子表中的每條記錄。我的意思是深度,有多少父母和祖先記錄存在。MySQL中獲取的紀錄,計數父母和祖先記錄深度
拿這個數據例如...
post_id parent_post_id
------- --------------
1 null
2 1
3 1
4 2
5 4
的數據表示這個層次...
1
|_ 2
| |_ 4
| |_ 5
|_ 3
查詢的結果應該是...
post_id depth
------- -----
1 0
2 1
3 1
4 2
5 3
提前致謝!
剛剛發現這個帖子。好像我被困在保持自嵌套模式的列深度是不恰當的:http://stackoverflow.com/questions/1195863/mysql-calculate-depth-in-a-parent-child-model – Nate 2010-05-13 02:17:11