start with connect by level 实现树

具体的sql为
select level treelevel,
eva.state,
t.id,
case
when t.parentid is null then
''
else
t.parentid || ''
end parentid,
t.summary,
t.score,
t.content,
t.standardscore,
t.selfAssessmentScore,
t.remark,
t.scoreReason,
t.ruleType,
case
when (select count(1) from [color=red]abc[/color] dt where dt.parentid = t.id) = 0 then
'1'
else
'0'
end leaf,
case
when level = 3 then
'0'
else
'1'
end expanded
from [color=red]abc[/color] t, [color=red]def[/color] eva
where t.defid = eva.id
and t.defid = #value#
start with t.id in (select id
from [color=red]abc[/color] d
where d.parentid is null
and d.defid = #value#)
connect by t.parentid = prior t.id ORDER SIBLINGS BY t.seq
****************************************************************
这段sql实现的是一个树形表,使用start with connect by prior level来实现,这个树有两张表:abc和def,abc是def的子表(abc.defid = def.id)。def存的是这个树叫什么名字,是哪个单位,哪个月份的树,abc存的是这个树具体的细则。
leaf和expanded映射成javaBean后是两个boolean类型的值,用来判断图标。
因此,排除def的干扰后,由abc的递归查询来实现这颗树:
[color=gray] start with t.id in (select id
from abc d
where d.parentid is null
and d.defid = #value#)
connect by t.parentid = prior t.id ORDER SIBLINGS BY t.seq[/color]

首先确定递归查询的范围:[color=gray]select id
from abc d
where d.parentid is null
and d.defid = #value#
defid=#value#[/color]这棵树的所有顶层节点(因为这颗树只有两层)

递归查询的下次的parentid是上次的id,根据seq字段进行兄弟姐妹排序(ORDER SIBLINGS BY)

由connect by生成的系统默认字段level来生成expanded字段的值 :arrow:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值