當前位置:首頁 » 編程語言 » 結構樹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')
是這樣的么??不曉得對不對.