1

TASK

I am currently trying to work out a viable structure for a simple application for the costing of jobs. I have decided to create one table to house all the operations and then link the operation together via a ParentID field. Below is a simplified structure of this table:

Simplified structure of my DB table

As you can see, the primary key is an integer field that does auto increment to keep it unique. Any operations that stem off another operation will have it under the parent ID field to create a simplistic breakdown of work flow. Also on this data table is a field for costs, this is a field that I am most interested in.

THE PROBLEM

I would like to run a query where I could throw in an operation ID and it would recursively run through that operation AND all of its children and its children's children etc. This would then accumulate all of the cost fields in the records that it retrieves. The only way I can think to do this is through recursive loops which in my opinion are not the best way to do this.

THE QUESTION

So, my question is, is there a way to do this without recursive loops? If there is not, can anyone suggest the cleanest and quickest way with the loops?

4

1 回答 1

0

根据定义,这种查询是递归的。无法使用该表结构获取该信息。

可以创建另一个表来存储所有层次结构信息。在插入 an时,Operation您必须递归地添加父母、祖父母、祖父母、祖父母,这也可能不是一个好主意,因为表格会很快变得非常大。不过,这会使查询变得更加简单。

附带说明:我建议将ParentID ParentOperationID. ParentID太笼统了。

于 2013-09-16T11:07:51.010 回答