首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求树形构造选择的SQL语句

2012-09-22 
求树形结构选择的SQL语句一个表SQL codetb_tree(id int,treecode varchar)/*数据如:idtreecode10000010110

求树形结构选择的SQL语句
一个表 

SQL code
tb_tree(id int,treecode varchar)/*数据如:  id      treecode10000      010110010      01010210013      01010310022      0101020420011      010220071      010203*/


根据id来选择,如果传id=10000,根据treecode,取出其下面的所有id(10000,10010,10013),求最简单的SQL语句。



[解决办法]
感觉LZ描述的还不够详细,如id=1000,treecode为0101,它的叶节点是哪些,根据什么来取?
[解决办法]
SQL code
declare @id varchar(20)set @id='10000'select * from tb_tree a,(select * from tb_tree where id=@id) bwhere a.treecode like b.treecode+'%'
[解决办法]
SQL code
select * from tb_treewhere treecode like (select treecode from tb_tree where id=2)+'%'
[解决办法]
SQL code
CREATE TABLE t1(    id INT,    treecode VARCHAR(20))INSERT INTO t1SELECT 10000,      '0101' UNION ALLSELECT 10010,      '010102' UNION ALLSELECT 10013,      '010103' UNION ALLSELECT 10022,      '01010204' UNION ALLSELECT 20011,      '0102' UNION ALLSELECT 20071,      '010203'SELECT * FROM t1SELECT a.* FROM t1 AS a INNER JOIN t1 AS b ON CHARINDEX(b.treecode,a.treecode)>0 AND b.id=10000-------------------------------id    treecode10000    010110010    01010210013    01010310022    01010204 

热点排行