我有一张桌子:
id type_name type_reportToId
1 Administrator 0
2 Guest GUEST 1
3 Landlord 1
4 Property Manager 3
13 Manager Manager 3
14 Asst. Property Manager 13
我需要“报告到层次结构”的ID为14号的结果。例如:id 14必须报告13,13必须报告3,3必须报告1 ...喜欢明智。我需要在mysql中查询。有谁能够帮我 ?
答案 0 :(得分:0)
您最好在应用程序逻辑中而不是在查询中执行此操作。
伪代码
query=SELECT id, type_name, type_reportToId FROM table ORDER BY id ASC, report_toId ASC
// loop through rows
currentParent = 0
processed = 0
hierarachyLevel = 0
while ( processed < foundrows )
{
while ( row = fetchrow( dbResult ) )
{
if ( row[type_reportToId] == currentParent )
{
resultHierarchy[hierarchyLevel] = row
currentParent = row[id]
processed++
hierarchyLevel++
dataseek( dbResult, 0 )
break
}
}
}