RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
用nosql实现树结构,树结构sql语句

怎样在 MySQL 表中存储树形结构数据

在 MySQL 表中存储树形结构数据:

我们提供的服务有:成都网站建设、网站制作、微信公众号开发、网站优化、网站认证、汉阳ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的汉阳网站制作公司

一般比较普遍的就是四种方法:(具体见 SQL Anti-patterns这本书)

Adjacency List:每一条记录存parent_id

Path Enumerations:每一条记录存整个tree path经过的node枚举

Nested Sets:每一条记录存 nleft 和 nright

Closure Table:维护一个表,所有的tree path作为记录进行保存。

怎样用数据库做成一个树结构

就是表需要有一个主键,比如 ID

然后还有一个字段,叫 父编号, 比如 PID

再加上其他的字段,剩下的就是写 SQL 的事情了。

例如下面这个表,就是一个可以做树状的。

CREATE TABLE test_tree (

test_id INT,

pid INT,

test_val VARCHAR(10),

PRIMARY KEY (test_id)

);

建立一个树形结构的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

oracle sql语句 显示树形结构

那个“热心网友”回答是对的, 你这里的数据都是散放在几个表里的, 不需要用start with connect by

直接一个表或者视图, 列出son, father, grand_father, grand grand father即可。

唯一需要补充的就是, 需要用outter join, 因为grand_father 不需要有son. 但是数据依然要列出。

select s.son_id, s.father_id, f.grand_father_id, g.grand_grand_faterh_id

from son s, father f, grand_father g

where s.father_id(+)=f.fatherid

and f.grand_father_id(+)=g.grand_father_id

在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 @@rowcount0

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

/*--结果

中国

----北京

----上海

----江苏

--------苏州

------------常熟

--------南京

--------无锡

美国

----纽约

----旧金山

加拿大

--*/

在sqlserver中实现树形结构中根、子节点数据的添加、修改

这个很好实现!就是添加和修改操作:

增加节点:首先要获取(在修改页面)或指定(在sql语句中)parentid的值,然后插入数据就可以啊!

修改节点:可以根据menuid,修改节点的menuName或者parentid或者是同时修改它们俩。

有问题请留言!


文章标题:用nosql实现树结构,树结构sql语句
网页URL:http://scyingshan.cn/article/hdishe.html