树形结构统一使用下面的测试表与测试数据
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, 'ASPNET');
INSERT INTO test_tree VALUES(5, 1, 'VBNET');
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 afiedtbuf
1 SELECT
2 LPAD(' ', 2(LEVEL-1)) || test_val AS test_val
3 FROM
4 test_tree
5 START WITH
6 test_id IN (1, 6, 10)
7 CONNECT BY PRIOR test_id = pid
SQL> /
TEST_VAL
-----------------------------------------------------------
NET
C#
J#
ASPNET
VBNET
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> Ttest_id,
16> Tpid,
17> Ttest_val,
18> CTELev + 1
19> FROM
20> test_tree T INNER JOIN StepCTE CTE
21> ON Tpid = CTEtest_id
22> )
23> SELECT
24> test_id, pid, test_val, Lev
25> FROM StepCTE;
26> go
test_id pid test_val Lev
----------- ----------- ---------- -----------
1 NULL NET 1
6 NULL J2EE 1
10 NULL Database 1
11 10 DB2 2
12 10 MySQL 2
13 10 Oracle 2
14 10 SQL Server 2
20 14 T-SQL 3
15 13 PL/SQL 3
16 15 Function 4
17 15 Procedure 4
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 ASPNET 2
5 1 VBNET 2
(20 行受影响)
如果树的层数固定就可以用语句查询,但效率比较低。例如你说的三层:
select id,v2name+name from t1 inner join
(select id,v1name+name as name from t1 inner join
(select id,name from t1 where parentid = 0) v1 on t1parentid = v1id) v2 on t1parentid = v2id
jsp从mysql数据库读取数据,并填充到树形结构菜单并展现出来的实现方法:
1、引入jquerytreeviewjs树控件
<script type="text/javascript" src="jquery/easyui/jqueryminjs"></script>
<script type="text/javascript" src="jquery/easyui/jqueryeasyuiminjs"></script>
2、jsp页面中获取后台mysql数据,并传到jsp页面来
<%
// 数据库的名字
String dbName = "zap";
// 登录数据库的用户名
String username = "sa";
// 登录数据库的密码
String password = "123";
// 数据库的IP地址,本机可以用 localhost 或者 127001
String host = "127001";
// 数据库的端口,一般不会修改,默认为1433
int port = 1433;
String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";user=" + username
+ ";password=" + password;
//
//声明需要使用的资源
// 数据库连接,记得用完了一定要关闭
Connection con = null;
// Statement 记得用完了一定要关闭
Statement stmt = null;
// 结果集,记得用完了一定要关闭
以上就是关于怎么往数据库里插入一个树形结构的表,并且用一句SQL语句将其遍历出来全部的内容,包括:怎么往数据库里插入一个树形结构的表,并且用一句SQL语句将其遍历出来、如何用sql语句实现树形的数据库表查询、数据库中树形结构怎么样在jsp中显示等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)