经典树型表结构

经典树型表结构

create table t_tree

(

  id       number(8) not null,

  pid      number(8) default 0 not null,--上级节点,0表示1一级根节点

  sort_no  number(8),--树排序号

  id_level number(2),--树层级

  sub_sort number(4),--同级排序号

  constraint pk_t_tree primary key (id)

);

insert into t_tree (ID, PID, SORT_NO, ID_LEVEL, SUB_SORT)values (1, 0, 1, 1, 5);

insert into t_tree (ID, PID, SORT_NO, ID_LEVEL, SUB_SORT)values (11, 1, 2, 2, 5);

insert into t_tree (ID, PID, SORT_NO, ID_LEVEL, SUB_SORT)values (111, 11, 3, 3, 5);

insert into t_tree (ID, PID, SORT_NO, ID_LEVEL, SUB_SORT)values (112, 11, 4, 3, 10);

insert into t_tree (ID, PID, SORT_NO, ID_LEVEL, SUB_SORT)values (113, 11, 5, 3, 15);

insert into t_tree (ID, PID, SORT_NO, ID_LEVEL, SUB_SORT)values (12, 1, 6, 2, 10);

insert into t_tree (ID, PID, SORT_NO, ID_LEVEL, SUB_SORT)values (13, 1, 7, 2, 15);

insert into t_tree (ID, PID, SORT_NO, ID_LEVEL, SUB_SORT)values (131, 13, 8, 3, 5);

insert into t_tree (ID, PID, SORT_NO, ID_LEVEL, SUB_SORT)values (132, 13, 9, 3, 10);

insert into t_tree (ID, PID, SORT_NO, ID_LEVEL, SUB_SORT)values (133, 13, 10, 3, 15);

 

全显示三级,根据sort_no整棵树排序

select * from t_tree order by sort_no;

123248_Zfoc_1245084.jpg

仅显示两级,根据id_level<=2查询

select * from t_tree where id_level<=2 order by sort_no;

123307_neiX_1245084.jpg

id_level等用于connect by语法中的level

select t.*, level from t_tree t connect by prior id = pid start with id = 1
 order by sort_no;

123329_H6PA_1245084.jpg

在java中根据sub_sort排序+pid即可构造出JTree对象

select * from t_tree order by sub_sort;

转载于:https://my.oschina.net/h2do/blog/268135

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值