说我有包含的字段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