怎么往数据库里插入一个树形结构的表,并且用一句SQL语句将其遍历出来

怎么往数据库里插入一个树形结构的表,并且用一句SQL语句将其遍历出来,第1张

树形结构统一使用下面的测试表与测试数据

CREATE TABLE test_tree (

test_id INT,

pid INT,

test_val VARCHAR(10),

PRIMARY KEY (test_id)

)

INSERT INTO test_tree VALUES(1, NULL, '.NET')

INSERT INTO test_tree VALUES(2, 1, 'C#')

INSERT INTO test_tree VALUES(3, 1, 'J#')

INSERT INTO test_tree VALUES(4, 1, 'ASP.NET')

INSERT INTO test_tree VALUES(5, 1, 'VB.NET')

INSERT INTO test_tree VALUES(6, NULL, 'J2EE')

INSERT INTO test_tree VALUES(7, 6, 'EJB')

INSERT INTO test_tree VALUES(8, 6, 'Servlet')

INSERT INTO test_tree VALUES(9, 6, 'JSP')

INSERT INTO test_tree VALUES(10, NULL, 'Database')

INSERT INTO test_tree VALUES(11, 10,'DB2')

INSERT INTO test_tree VALUES(12, 10,'MySQL')

INSERT INTO test_tree VALUES(13, 10,'Oracle')

INSERT INTO test_tree VALUES(14, 10,'SQL Server')

INSERT INTO test_tree VALUES(15, 13,'PL/SQL')

INSERT INTO test_tree VALUES(16, 15,'Function')

INSERT INTO test_tree VALUES(17, 15,'Procedure')

INSERT INTO test_tree VALUES(18, 15,'Package')

INSERT INTO test_tree VALUES(19, 15,'Cursor')

INSERT INTO test_tree VALUES(20, 14,'T-SQL')

Oracle

使用 START WITH CONNECT BY

语句实现树状查询

SQL>ed

Wrote file afiedt.buf

1 SELECT

2LPAD(' ', 2*(LEVEL-1)) || test_val AS test_val

3 FROM

4test_tree

5 START WITH

6test_id IN (1, 6, 10)

7* CONNECT BY PRIOR test_id = pid

SQL>/

TEST_VAL

-----------------------------------------------------------

.NET

C#

J#

ASP.NET

VB.NET

J2EE

EJB

Servlet

JSP

Database

DB2

TEST_VAL

-----------------------------------------------------------

MySQL

Oracle

PL/SQL

Function

Procedure

Package

Cursor

SQL Server

T-SQL

20 rows selected.

SQL Server

使用 Common Table Expression (CTE) 来实现 递归调用。

1>WITH StepCTE

2>AS

3>(

4>SELECT

5> test_id,

6> pid,

7> test_val,

8> 1 as Lev

9>FROM

10> test_tree

11>WHERE

12> test_id IN (1,6,10)

13>UNION ALL

14>SELECT

15> T.test_id,

16> T.pid,

17> T.test_val,

18> CTE.Lev + 1

19>FROM

20> test_tree T INNER JOIN StepCTE CTE

21> ON T.pid = CTE.test_id

22>)

23>SELECT

24> test_id, pid, test_val, Lev

25>FROM StepCTE

26>go

test_id pid test_val Lev

----------- ----------- ---------- -----------

1NULL .NET 1

6NULL J2EE 1

10NULL Database 1

11 10 DB2 2

12 10 MySQL2

13 10 Oracle 2

14 10 SQL Server 2

20 14 T-SQL3

15 13 PL/SQL 3

16 15 Function 4

17 15 Procedure4

18 15 Package 4

19 15 Cursor 4

7 6 EJB 2

8 6 Servlet 2

9 6 JSP 2

2 1 C# 2

3 1 J# 2

4 1 ASP.NET 2

5 1 VB.NET 2

(20 行受影响)

TreeView的数据存储格式一般如下:

节点编号,节点名称,节点值,上级节点编号

遍历TreeView一般用递归算法

public void LoadTree(TreeNode tn)

{

if(tn == null) //加载根节点

{

//从数据库获取上级节点编号=0的DataSet

foreach(DataRow dr in ds.Tables[0].Rows)

{

TreeNode newNode=new TreeNode(dr["节点名称"] , dr["节点编号"])

tn.Nodes.Add(newNode)

LoadTree(newNode)//递归

}

}

else

{

//获取当前节点的下一层节点DataSet

foreach(DataRow dr in ds.Tables[0].Rows)

{

TreeNode newNode=new TreeNode(dr["节点名称"] , dr["节点编号"])

tn.Nodes.Add(newNode)

LoadTree(newNode)//递归

}

}

}

//是类似这种吗

//控制层使用JSONArray jsonObject=JSONArray.fromObject()转换

Map<String,Object> map = new HashMap<String, Object>()

     map.put("id","1")

     map.put("text","实验外国语学校")

     List<Map<String,Object>> fatherList=new ArrayList<Map<String,Object>>()

     List<Map<String,Object>> list=new ArrayList<Map<String,Object>>()

     for(Bean bean:list){

         if("1".equals(list.getParent_level())){

    

         Map<String,Object> map2=new HashMap<String, Object>()

    

         map2.put("id",list.getId())

    

         map2.put("text",list.getName())

    

         list.add(map2)

    

         }

    

     }

     map.put("children",list)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9970463.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存