当前位置:首页 » 编程语言 » sql查询树

sql查询树

发布时间: 2022-08-21 15:25:29

1. sql怎么实现树查询

表格式如下:

cid pid cname
1 0 董事长
2 1 CEO
3 2 销售经理
4 2 IT经理
5 2 运营经理
6 3 销售主管
7 4 IT主管
8 5 运营主管
9 6 业务员
10 7 程序员
11 8 运营员

create function get_detail(
@id int
)returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.cid,@l
from table_2 a,@re b
where a.pid=b.id and b.level=@l-1
end
return
end
go

--调用(查询所有的子)
select a.*,level=b.level from table_2 a,get_detail(2)b where a.cid=b.id

2. 关于SQL查询树结构数据的语句

方法1:
是否可以有代表层次的字段,若有,直接根据层次字段查询;
方法2:
是否存在以下假设:
非父级的dept_uid的 不可能 在 parent_uid 中出现。
如果假设成立,在查出的所有dept_uid中去掉,所有在在 parent_uid 中出现id,
剩下的就是你要的了。

3. sql 查询 tree

很高兴回答你的问题
根据你的需求,写的SQL如下:
select id,LPAD(name,LENGTH(name)+(LEVEL*10),' ')
from table_name
where 1=1 start with fid is null connect by PRIOR id=fid;
根据你的问题补充:
select id from table_name where id not in(
select id
from table_name
where 1=1 start with id=1 connect by PRIOR id=fid);上边这个sql就查询出来当id=1的时候,能作为它上级的id,
希望对你有所帮助!

4. 如何用sql语句实现树形的数据库表查询

如果树的层数固定就可以用语句查询,但效率比较低。例如你说的三层:
select id,v2.name+name from t1 inner join
(select id,v1.name+name as name from t1 inner join
(select id,name from t1 where parentid = 0) v1 on t1.parentid = v1.id) v2 on t1.parentid = v2.id

5. sql 查询树形数据。

如果树的层数固定就可以用语句查询,但效率比较低。例如你说的三层:
select id,v2.name+name from t1 inner join
(select id,v1.name+name as name from t1 inner join
(select id,name from t1 where parentid = 0) v1 on t1.parentid = v1.id) v2 on t1.parentid = v2.id

6. 关于sql server中的树查询

就用with递归,根节点等级为0,递归时每级+1

7. SQL语句实现子孙树查询经典实例

下面介绍的SQL语句非常经典,该SQL语句实现子孙树查询,该SQL语句可以直接在查询分析器中执行,供您参考。
--生成表
create table MENU(id int,mname char(50),parent int)
--插入数据
insert into MENU
select 1,'新闻',Null union all
select 2,'房产',Null union all
select 3,'科技新闻',1 union all
select 4,'社会新闻',1 union all
select 5, 'IT新闻',3 union all
select 6, '航天新闻',3
--实现查询新闻子孙树
Declare @s varchar(1000)
select @s=','+cast(id as varchar(20))+'' from MENU where id=1 while @@rowCount>0
--charindex:返回字符串中指定表达式的起始位置
select @s=@s+','+cast(id as varchar) from MENU
where charindex(','+cast(id as varchar)+',',@s+',')=0

and charindex(','+cast(parent as varchar)+',',@s+',')>0
select * from MENU where charindex(','+cast(id as varchar)+',',@s+',')>0
--删除表
drop table MENU

8. 如何用SQL解决树查询问题,急!!!

oracle中的select语句可以用START WITH...CONNECT BY PRIOR子句实现递归查询,connect by 是结构化查询中用到的,其基本语法是:

select * from tablename start with cond1
connect by prior cond2
where cond3;

简单说来是将一个树状结构存储在一张表里,比如一个表中存在两个字段:
id,parentid。那么通过表示每一条记录的parent是谁,就可以形成一个树状结构。

用上述语法的查询可以取得这棵树的所有记录。

其中COND1是根结点的限定语句,当然可以放宽限定条件,以取得多个根结点,实际就是多棵树。

COND2是连接条件,其中用PRIOR表示上一条记录,比如 CONNECT BY PRIOR ID=PRAENTID就是说上一条记录的ID是本条记录的PRAENTID,即本记录的父亲是上一条记录。

COND3是过滤条件,用于对返回的所有记录进行过滤。

9. sql组织机构树查询

组织等级是四层,应该显示四列才完整

createtablestat(codevarchar(10),parentvarchar2(10),slevelvarchar(10));
INSERTINTOSTATVALUES('中国','0','国家');
INSERTINTOSTATVALUES('浙江省','中国','省');
INSERTINTOSTATVALUES('江苏省','中国','省');
INSERTINTOSTATVALUES('杭州市','浙江省','市');
INSERTINTOSTATVALUES('南京市','江苏省','市');
INSERTINTOSTATVALUES('西湖区','杭州市','县/区');
INSERTINTOSTATVALUES('玄武区','南京市','县/区');
SELECTA.PARENT,A.CODE,B.CODE,C.CODE
FROMSTATA,STATB,STATC
WHEREA.CODE=B.PARENT
ANDB.CODE=C.PARENT
ANDA.PARENT='中国';
PARENTCODECODECODE
----------------------------------------
中国浙江省杭州市西湖区
中国江苏省南京市玄武区
热点内容
android获取手机联系人 发布:2025-05-14 10:59:30 浏览:140
电脑怎么选择配置 发布:2025-05-14 10:46:12 浏览:325
电脑怎么不显示手机连接服务器失败 发布:2025-05-14 10:42:28 浏览:9
安卓如何下载lv手游 发布:2025-05-14 10:35:45 浏览:383
pythondict添加key 发布:2025-05-14 10:33:59 浏览:382
柱子箍筋加密区长度 发布:2025-05-14 10:18:29 浏览:352
云服务器和内网穿透哪个好 发布:2025-05-14 10:16:41 浏览:627
安徽新能源网络配置是什么 发布:2025-05-14 10:06:24 浏览:631
pinode搭建服务器 发布:2025-05-14 10:04:23 浏览:4
电脑服务器ip名称 发布:2025-05-14 10:01:09 浏览:749