数据库是什么数据库
我先假设你是 Oracle 吧。
CREATE TABLE test_tree (
test_id INT NOT NULL,
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');
使用 START WITH CONNECT BY 语句实现树状查询
通过根节点 向下查询子节点
SELECT
LPAD(' ', 2(LEVEL-1)) || test_val AS test_val
FROM
test_tree
START WITH
test_id IN (1, 6, 10)
CONNECT BY PRIOR test_id = pid;
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
import javautilArrayList;
import javautilIterator;
import javautilList;
import orgdom4jDocument;
import orgdom4jDocumentException;
import orgdom4jElement;
import orgdom4jioSAXReader;
import orgdom4jtreeDefaultAttribute;
/
Java递归遍历XML所有元素
@author Administrator
@version [版本号, Apr 13, 2010]
@see [相关类/方法]
@since [产品/模块版本]
/
public class XmlParser
{
// private static Map xmlmap = new HashMap();
//存储xml元素信息的容器
private static ArrayList<Leaf> elemList = new ArrayList<Leaf>();
public static void main(String args[])
throws DocumentException
{
XmlParser test = new XmlParser();
String path = "C:/axml";
// 读取XML文件
SAXReader reader = new SAXReader();
Document doc = readerread(path);
// 获取XML根元素
Element root = docgetRootElement();
testgetElementList(root);
String x = testgetListString(elemList);
Systemoutprintln("-----------解析结果------------\n" x);
}
用标准sql的with实现递归查询(sql2005以上肯定支持,sql2000不清楚是否支持):
with subqry(id,name,pid) as (
select id,name,pid from test1 where id = 5
union all
select test1id,test1name,test1pid from test1,subqry
where test1pid = subqryid
)
select from subqry;
以上就是关于数据库表有3列,Id,Name,ParentId,已从数据库读出数据,如何递归成一棵树全部的内容,包括:数据库表有3列,Id,Name,ParentId,已从数据库读出数据,如何递归成一棵树、如何通过递归生成xml树,求解、怎么获取递归树的所有叶子节点sql等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)