当前位置:首页 » 编程语言 » 结构树sql
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

结构树sql

发布时间: 2022-11-06 11:40:39

① 树形表结构,sql怎么按父级名分组

写成存储过程会好一些,单句sql的话麻烦的,如果需要的话可以联系我。 另外,树形结构的数据库表,可以在添加一列为它的深度,也就是它的级数,这样会很方便。

② 求教oracle怎么用一个SQL查询多层树形结构

select * from 表 m start with m.id=1 connect by m.parent=prior m.id;

③ SQL更新树结构(计算产品标准工时)

你的意思是父产品的工时由自身和子产品的工时共同决定吗?
如果一定要在sql里操作了吧,那就写个触发器吧。在子产品被update时,根据新数据去更新父产品

④ sql server怎么对树形结构表的节点进行拼接

DECLARE @temp nvarchar(MAX)='',@pid bigint=3;
WHILE @pid<>0 BEGIN IF @temp=''
SELECT @temp=TypeName,@pid=ParentId
FROM [dbo].[Test]
WHERE Id=@pid;
ELSE
SELECT @temp=(TypeName+'->'+@temp),@pid=ParentId
FROM [dbo].[Test]
WHERE Id=@pid;
END;
SELECT @temp AS TypeName;

⑤ 在MySql下,怎么用SQL语句遍历一个树结构

f exists (select * from dbo.sysobjects where id = object_id(N'[tb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [tb]
GO

--示例数据
create table [tb]([id] int identity(1,1),[pid] int,name varchar(20))
insert [tb] select 0,'中国'
union all select 0,'美国'
union all select 0,'加拿大'
union all select 1,'北京'
union all select 1,'上海'
union all select 1,'江苏'
union all select 6,'苏州'
union all select 7,'常熟'
union all select 6,'南京'
union all select 6,'无锡'
union all select 2,'纽约'
union all select 2,'旧金山'
go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_id]') and xtype in (N'FN', N'IF', N'TF'))

drop function [dbo].[f_id]
GO

/*--树形数据处理

级别及排序字段

--邹建 2003-12(引用请保留此信息)--*/

/*--调用示例

--调用函数实现分级显示

select replicate('-',b.[level]*4)+a.name

from [tb] a,f_id()b

where a.[id]=b.[id]

order by b.sid
--*/
create function f_id()
returns @re table([id] int,[level] int,sid varchar(8000))
as
begin

declare @l int

set @l=0

insert @re select [id],@l,right(10000+[id],4)

from [tb] where [pid]=0

while @@rowcount>0

begin

set @l=@l+1

insert @re select a.[id],@l,b.sid+right(10000+a.[id],4)

from [tb] a,@re b

where a.[pid]=b.[id] and b.[level]=@l-1

end

return
end
go

--调用函数实现分级显示
select replicate('-',b.[level]*4)+a.name
from [tb] a,f_id()b
where a.[id]=b.[id]
order by b.sid
go

--删除测试
drop table [tb]
drop function f_id
go

/*--结果
中国
----北京
----上海
----江苏
--------苏州
------------常熟
--------南京
--------无锡
美国
----纽约
----旧金山
加拿大

--*/

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

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

⑦ oracle sql语句 显示树形结构

设计的表结构不合理,怎么能用对象设计表呢。组多两个字段, people_id,parent_id
只关心他们的层级关系即可。
查询出来是
select t.people_id, t.parent_id from family t
start with t.people_id is null connect by nocycle prior t.people_id=t.parent_id。

真多多层级查询毫无压力。另外 对 函数的理解多参考下语法用途。nocycle 防止死循环。可以举一反三,自己试一下 start with 中 people_id 与 parent_id 互换位置,会有额外收获,多动手。祝你oracle 之旅愉快~

⑧ sql里树形结构分组排序

createtableT1(thisvarchar(10),parentvarchar(10))
insertintoT1(this,parent)
values('id1',null)
,('id2',null)
,('id3','id1')
,('id4','id2')
,('id5','id3')
,('id6','id3')
,('id7','id4')
,('id8','id7')

--sqlserver的cte功能
withtree(this,parent,root,depth)as(
selectthis,parent,thisasroot,
unionall
selecta.this,a.parent,b.root,b.depth+1asdepthfromT1a,treebwherea.parent=b.this
)
selectthis,parent,root,depth
fromtree
orderbyroot,depth,this

⑨ 建立一个树形结构的SQL表 3实现的功能

主要是要有ID,PID两个字段,下面是我用的一个表,仅供参考:
CREATE TABLE [dbo].[Sys_Menu](
[ID] [int] NOT NULL,
[Code] [varchar](50) NOT NULL,
[PID] [int] NOT NULL,
[Name] [nvarchar](50) NULL,
[Url] [varchar](100) NULL,
[STATE] [bit] NOT NULL,
[IsSelected] [bit] NULL
) ON [PRIMARY]

GO

⑩ sql 查询树型结构

select parent,child from test where parent in(select child from test where parent='a')
是这样的么??不晓得对不对.