还是SQL,感觉有点难度

sohighthesky 2009-09-03 07:05:58
环境Access,

CaseTypes案例分类,级别不限的那种,无限树那样的
CID int,
CPID int,
CName varchar(50)

表CaseInfo案例信息
InfoID int,
InfoName varchar(50),
CID int,所属类别的ID,


问题,查询某个类别ID下的案例信息

不好表达 了
就是案例信息里面的ID,只要他的父ID,爷ID。。。,向上中包含这个ID,就查出来


不知道能不能有办法?

路过SQL牛人的来看看,
...全文
186 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohighthesky 2009-09-05
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 dawugui 的回复:]
这里是SQL版,楼主可能来错地方了.
[/Quote]
没有,我从c#又转过来 的


自己解决,结帖,非常感谢LS提供的MS-Sqlserver中 方法
dawugui 2009-09-04
  • 打赏
  • 举报
回复
这里是SQL版,楼主可能来错地方了.
sohighthesky 2009-09-04
  • 打赏
  • 举报
回复
转到c#来看看,

哪个帮忙一起看看15L我说的思路啊
WWWWA 2009-09-04
  • 打赏
  • 举报
回复
在ACCESS中,只能用VBA递归调用,SQL语句不行
ACMAIN_CHM 2009-09-03
  • 打赏
  • 举报
回复
ACCESS中的T-SQL仅用SQL语句是无法实现的,只能通过程序来实现

或用ACCESS中的VBA来实现,或用你的开发语言来递归实现。
htl258_Tony 2009-09-03
  • 打赏
  • 举报
回复
转到access提问一下,你的问题会较快解决。
sohighthesky 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 licry01 的回复:]
用ACCESS, 你怎么跑SQL Server区来了?你应该去VB区
ACCESS,不支持用户定义函数和存储过程, 不过它是基于VBA的,支持VBS的函数,这很有诱惑哦。

数据量不大, 你还是在程序里面用函数递归检索来实现你要的结果吧, sql语句没法实现, 俺早年弄网站bbs论坛树型回帖的时候就一个sub递归搞定。

如果可以更改数据结构的话, 我可以给你个思路, 大致如下:

表CaseTypes加一个字段, 保存每个节点的路径(path)

CID int,
CPID int,
CName varchar(50),
CPath Memo (用来保存对应的父节点的路径,(CPath + '_' + CID + '_')标识该结点的路径。 该字段最大容量是2G, 你的级别不会深到把它撑破吧)

用下面数据来说明如何操作
CID      CName        CPID      CPath
1        Node01        0        _0_
2        Node02        1        _0_1_
3        Node03        2        _0_1_2_
4        Node04        3        _0_1_2_3_

Node01
------Node02
      ------Node03
            ------Node03


查找节点ID是1的所有子节点:
SELECT  * from CaseTypes where instr(cstr(CPath),'_1_')>0 ;
CID      CName        CPID      CPath
2        Node02        1        _0_1_
3        Node03        2        _0_1_2_
4        Node04        3        _0_1_2_3_


查找节点ID是2的所有子节点:
SELECT  * from CaseTypes where instr(cstr(CPath),'_2_')>0 ;
CID      CName        CPID      CPath
3        Node03        2        _0_1_2_
4        Node04        3        _0_1_2_3_


查找节点ID是3的所有父节点:
SELECT  * from CaseTypes where CID <>3 and instr('_0_1_2_',cstr(CPath))=1 ;
CID      CName        CPID      CPath
1        Node01        0        _0_
2        Node02        1        _0_1_



[/Quote]

是SQL问题啊,所以就来这里了,,,

你说的这个不符合要求,数据是已经存在了,我不可能给每个记录加一列,,


waiting................
licry01 2009-09-03
  • 打赏
  • 举报
回复
用ACCESS, 你怎么跑SQL Server区来了?你应该去VB区
ACCESS,不支持用户定义函数和存储过程, 不过它是基于VBA的,支持VBS的函数,这很有诱惑哦。

数据量不大, 你还是在程序里面用函数递归检索来实现你要的结果吧, sql语句没法实现, 俺早年弄网站bbs论坛树型回帖的时候就一个sub递归搞定。

如果可以更改数据结构的话, 我可以给你个思路, 大致如下:

表CaseTypes加一个字段, 保存每个节点的路径(path)

CID int,
CPID int,
CName varchar(50),
CPath Memo (用来保存对应的父节点的路径,(CPath + '_' + CID + '_')标识该结点的路径。 该字段最大容量是2G, 你的级别不会深到把它撑破吧)

用下面数据来说明如何操作
CID CName CPID CPath
1 Node01 0 _0_
2 Node02 1 _0_1_
3 Node03 2 _0_1_2_
4 Node04 3 _0_1_2_3_

Node01
------Node02
------Node03
------Node03


查找节点ID是1的所有子节点:
SELECT * from CaseTypes where instr(cstr(CPath),'_1_')>0 ;
CID CName CPID CPath
2 Node02 1 _0_1_
3 Node03 2 _0_1_2_
4 Node04 3 _0_1_2_3_


查找节点ID是2的所有子节点:
SELECT * from CaseTypes where instr(cstr(CPath),'_2_')>0 ;
CID CName CPID CPath
3 Node03 2 _0_1_2_
4 Node04 3 _0_1_2_3_


查找节点ID是3的所有父节点:
SELECT * from CaseTypes where CID<>3 and instr('_0_1_2_',cstr(CPath))=1 ;
CID CName CPID CPath
1 Node01 0 _0_
2 Node02 1 _0_1_


sohighthesky 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 dingnifei123 的回复:]
引用 12 楼 sohighthesky 的回复:
大虾们怎么都没看到第一句呢?
Access可以用函数么?

伤自尊了!!

不过大侠们争先恐后的热情是很值得肯定的!!
[/Quote]

哎,开始高兴的看到这么多答案,可是回来一看却不能用,伤心啊,,,
不过还是非常感谢,

继续等人解答,,

我看了下Access好像不支持自定义函数,,

现在有个想法就是先根据ID却查ID和PID然后取出来用c#递归,看哪些符合要求的,再到Access里去查询,,,可是还是不明白能怎么用c#去递归呢?
Dingnifei123 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 sohighthesky 的回复:]
大虾们怎么都没看到第一句呢?
Access可以用函数么?
[/Quote]
伤自尊了!!

不过大侠们争先恐后的热情是很值得肯定的!!
Dingnifei123 2009-09-03
  • 打赏
  • 举报
回复
牛人满天飞的感觉,
学习了!!
sohighthesky 2009-09-03
  • 打赏
  • 举报
回复


大虾们怎么都没看到第一句呢?
Access可以用函数么?
sohighthesky 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fredrickhu 的回复:]
SQL code--2000的用函数:CREATETABLE BOM(PIDINT,IDINT)INSERTINTO BOMSELECT801,101INSERTINTO BOMSELECT801,102INSERTINTO BOMSELECT801,103INSERTINTO BOMSELECT801,601INSERTINTO BOMSELECT601,101INSERTINTO BOMSEL¡­
[/Quote]

不好意思,请看问题第一句,还是谢了
soft_wsx 2009-09-03
  • 打赏
  • 举报
回复
同志们都历害!顶贴!不知道能否在ACCESS中运行
soft_wsx 2009-09-03
  • 打赏
  • 举报
回复
/*
Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) Jul 9 2008 14:43:34 Copyright (c)
1988-2008 Microsoft Corporation Enterprise Evaluation Edition on Windows NT 5.1 <X86>
(Build 2600: Service Pack 3)
愿和大家共同进步
如有雷同、实属巧合
●●●●●2009-09-03 17:47:36.077●●●●●
 ★★★★★soft_wsx★★★★★
*/
--树型结构处理之双编号(广度深度排序)
if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0
drop table tb
create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
insert tb
select '0001',null,'云南省'
union all select '0002','0001','昆明市'
union all select '0003','0001','昭通市'
union all select '0009','0001','大理市'
union all select '0008',null,'四川省'
union all select '0004',null,'贵州省'
union all select '0005','0002','五华区'
union all select '0007','0002','水富县'
union all select '0006','0005','西园路192号'
union all select '0010','0006','金色梧桐'
union all select '0011','0010','科技有限公司'
union all select '0015','0007','两碗乡'
union all select '0013','0015','两碗村'
union all select '0012','0013','某跨国集团董事长'
union all select '0014','0008','成都市'

--select * from tb
--广度排序(先显示第一层节点,再显示第二次节点......)
--定义辅助表
declare @level_tb table(bh nvarchar(10),level int)
declare @level int
set @level=0
insert @level_tb(bh,level)
select ybh,@level from tb where ebh is null
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tb(bh,level)
select ybh,@level
from tb a,@level_tb b
where a.ebh=b.bh
and b.level=@level-1
end
select a.*,b.* from tb a,@level_tb b where a.ybh=b.bh order by level
/*
ybh ebh beizhu bh level
0001 NULL 云南省 0001 0
0008 NULL 四川省 0008 0
0004 NULL 贵州省 0004 0
0002 0001 昆明市 0002 1
0003 0001 昭通市 0003 1
0009 0001 大理市 0009 1
0014 0008 成都市 0014 1
0005 0002 五华区 0005 2
0007 0002 水富县 0007 2
0006 0005 西园路192号 0006 3
0015 0007 两碗乡 0015 3
0010 0006 金色梧桐 0010 4
0013 0015 两碗村 0013 4
0011 0010 科技有限公司 0011 5
0012 0013 某跨国集团董事长 0012 5
*/

--深度排序(模拟单编码法)
declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
declare @level int
set @level=0
insert @level_tt(ybh,ebh,level)
select ybh,ybh,@level from tb where ebh is null
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tt(ybh,ebh,level)
select a.ybh,b.ebh+a.ybh,@level
from tb a,@level_tt b
where a.ebh=b.ybh and b.level=@level-1
end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
from tb a,@level_tt b
where a.ybh=b.ybh
order by b.ebh
/*(无列名) ybh ebh beizhu ybh ebh level
----云南省 0001 NULL 云南省 0001 0001 0
----昆明市 0002 0001 昆明市 0002 00010002 1
----五华区 0005 0002 五华区 0005 000100020005 2
----西园路192号 0006 0005 西园路192号 0006 0001000200050006 3
----金色梧桐 0010 0006 金色梧桐 0010 00010002000500060010 4
----科技有限公司 0011 0010 科技有限公司 0011 000100020005000600100011 5
----水富县 0007 0002 水富县 0007 000100020007 2
----两碗乡 0015 0007 两碗乡 0015 0001000200070015 3
----两碗村 0013 0015 两碗村 0013 00010002000700150013 4
----某跨国集团董事长 0012 0013 某跨国集团董事长 0012 000100020007001500130012 5
----昭通市 0003 0001 昭通市 0003 00010003 1
----大理市 0009 0001 大理市 0009 00010009 1
----贵州省 0004 NULL 贵州省 0004 0004 0
----四川省 0008 NULL 四川省 0008 0008 0
----成都市 0014 0008 成都市 0014 00080014 1
*/



--查找子节点(包括本身节点和子节点)
declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
declare @level int
set @level=0
insert @level_tt(ybh,ebh,level)
select ybh,ybh,@level from tb where ybh='0005'
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tt(ybh,ebh,level)
select a.ybh,b.ebh+a.ybh,@level
from tb a,@level_tt b
where a.ebh=b.ybh and b.level=@level-1
end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
from tb a,@level_tt b
where a.ybh=b.ybh
order by b.ebh

/*
(无列名) ybh ebh beizhu ybh ebh level
----五华区 0005 0002 五华区 0005 0005 0
----西园路192号 0006 0005 西园路192号 0006 00050006 1
----金色梧桐 0010 0006 金色梧桐 0010 000500060010 2
----科技有限公司 0011 0010 科技有限公司 0011 0005000600100011 3
*/

--查的父节点(包括本身节点和所有的你节点)
declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
declare @level int
set @level=0
insert @level_tt(ybh,ebh,level)
select ybh,ebh,@level from tb where ebh='0005'
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tt(ybh,ebh,level)
select a.ebh,b.ebh+a.ebh,@level
from tb a,@level_tt b
where a.ybh=b.ybh and b.level=@level-1
end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
from tb a,@level_tt b
where a.ybh=b.ybh
order by b.ebh desc

/*
(无列名) ybh ebh beizhu ybh ebh level
----云南省 0001 NULL 云南省 0001 0005000500020001 3
----昆明市 0002 0001 昆明市 0002 000500050002 2
----五华区 0005 0002 五华区 0005 00050005 1
----西园路192号 0006 0005 西园路192号 0006 0005 0
*/
陌上花花 2009-09-03
  • 打赏
  • 举报
回复
帮顶
黄_瓜 2009-09-03
  • 打赏
  • 举报
回复
这个是刚写的
--> 测试数据:@tb
create table tb([user_group_id] int,[per_group_id] int,[user_group_name] varchar(10))
insert tb
select 1,0,'总管理员' union all
select 2,1,'一级管理员' union all
select 3,2,'二级管理员' union all
select 4,3,'三级管理员' union all
select 8,4,'四级管理员' union all
select 10,8,'五级管理员'

--2000
---创建函数
CREATE FUNCTION f_Cid(@name varchar(10))
RETURNS @t TABLE(ID varchar(10),Level int)
AS
BEGIN
DECLARE @Level int,@id int
SET @Level=1
set @id=(select [per_group_id] from tb where [user_group_name]=@name )
INSERT @t SELECT @ID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t SELECT a.[user_group_id],@Level
FROM tb a,@t b
WHERE a.[per_group_id]=b.id
AND b.Level=@Level-1
END
RETURN
END
GO

--调用函数查询二级管理员及其所有子节点
SELECT a.*
FROM tb a,f_Cid('二级管理员') b
WHERE a.[per_group_id]=b.id

/*
user_group_id per_group_id user_group_name
------------- ------------ ---------------
3 2 二级管理员
4 3 三级管理员
8 4 四级管理员
10 8 五级管理员

(4 行受影响)


*/


--2005

;with t as(
select * from tb where [user_group_name]='二级管理员'
union all
select a.* from tb a ,t where a.[per_group_id]=t.[user_group_id]

)
select * from t

/*
user_group_id per_group_id user_group_name
------------- ------------ ---------------
3 2 二级管理员
4 3 三级管理员
8 4 四级管理员
10 8 五级管理员

(4 行受影响)
*/
sohighthesky 2009-09-03
  • 打赏
  • 举报
回复

,这么快都有人了,非常感谢,回去再看
--小F-- 2009-09-03
  • 打赏
  • 举报
回复
--2000的用函数:
CREATE TABLE BOM(PID INT,ID INT)
INSERT INTO BOM SELECT 801,101
INSERT INTO BOM SELECT 801,102
INSERT INTO BOM SELECT 801,103
INSERT INTO BOM SELECT 801,601
INSERT INTO BOM SELECT 601,101
INSERT INTO BOM SELECT 601,105
INSERT INTO BOM SELECT 601,501
INSERT INTO BOM SELECT 501,106
INSERT INTO BOM SELECT 501,121
GO

CREATE FUNCTION F_GETROOT(@PID INT)
RETURNS INT
AS
BEGIN
DECLARE @ID INT
WHILE EXISTS(SELECT 1 FROM BOM WHERE ID=@PID)
BEGIN
SET @ID=@PID
SELECT @PID=PID FROM BOM WHERE ID=@ID
END
RETURN @PID
END
GO

SELECT PID=DBO.F_GETROOT(PID),ID FROM BOM
GO

/*
PID ID
----------- -----------
801 101
801 102
801 103
801 601
801 101
801 105
801 501
801 106
801 121
*/


DROP FUNCTION F_GETROOT
DROP TABLE BOM
GO




--生成测试数据
create table BOM_1(Item int,bom_head varchar(20),bom_child varchar(20),number int,products_attribute varchar(20))
insert into BOM_1 select 1 ,'A' ,'A1',1,'采购'
insert into BOM_1 select 2 ,'A' ,'A2',2,'生产'
insert into BOM_1 select 3 ,'A2','A3',3,'生产'
insert into BOM_1 select 4 ,'A2','A4',2,'采购'
insert into BOM_1 select 5 ,'A3','A5',2,'采购'
insert into BOM_1 select 6 ,'A3','A6',1,'采购'
insert into BOM_1 select 7 ,'B' ,'B1',1,'采购'
insert into BOM_1 select 8 ,'B' ,'B2',2,'生产'
insert into BOM_1 select 9 ,'B2','B3',3,'生产'
insert into BOM_1 select 10,'B2','B4',2,'采购'
insert into BOM_1 select 11,'B3','B5',2,'采购'
insert into BOM_1 select 12,'B3','B6',2,'采购'
go


--创建用户定义函数,用于取每个父节点下子节点的采购配置信息
create function f_stock(@bom_head varchar(20))
returns @t table(bom varchar(20),number int)
as
begin
declare @level int
declare @a table(bom varchar(20),number int,products_attribute varchar(20),[level] int)
set @level=1

if exists(select 1 from BOM_1 where bom_head=@bom_head)
insert into @a
select bom_child,number,products_attribute,@level
from BOM_1
where bom_head=@bom_head

while exists(select 1 from @a where [level]=@level and products_attribute='生产')
begin
set @level=@level+1
insert into @a(bom,number,products_attribute,[level])
select a.bom_child,a.number,a.products_attribute,@level
from BOM_1 a,@a b
where a.bom_head=b.bom and b.[level]=@level-1
end

insert into @t(bom,number) select bom,number from @a where products_attribute='采购'
return
end
go


--执行调用,取父节点'A'一个标准配置分解的采购信息及数量
select * from dbo.f_stock('A')






--生成测试数据
create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
insert into BOM select 1,0,NULL
insert into BOM select 2,1,NULL
insert into BOM select 3,1,NULL
insert into BOM select 4,2,NULL
insert into BOM select 5,3,NULL
insert into BOM select 6,5,NULL
insert into BOM select 7,6,NULL
go

--创建用户定义函数用于取每个父节点下子节点的采购配置信息
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
declare @i int
set @i = 1
insert into @t select ID,PID,@i from BOM where PID = @ID

while @@rowcount<>0
begin
set @i = @i + 1

insert into @t
select
a.ID,a.PID,@i
from
BOM a,@t b
where
a.PID=b.ID and b.Level = @i-1
end
return
end
go

--执行查询
select ID from dbo.f_getChild(3)
go

--输出结果
/*
ID
----
5
6
7
*/

--删除测试数据
drop function f_getChild
drop table BOM





创建用户定义函数,每个子节点de父节点的信息


--生成测试数据
create table BOM(ID int,parentID int,sClassName varchar(10))
insert into BOM values(1,0,'1111' )
insert into BOM values(2,1,'1111_1' )
insert into BOM values(3,2,'1111-1-1' )
insert into BOM values(4,3,'1111-1-1-1')
insert into BOM values(5,1,'1111-2' )

go

--创建用户定义函数,每个子节点de父节点的信息
create function f_getParent(@ID int)
returns varchar(40)
as
begin
declare @ret varchar(40)

while exists(select 1 from BOM where ID=@ID and parentID<>0)
begin
select @ID=b.ID,@ret=','+rtrim(b.ID)+isnull(@ret,'')
from
BOM a,BOM b
where
a.ID=@ID and b.ID=a.parentID
end

set @ret=stuff(@ret,1,1,'')
return @ret
end
go

--执行查询
select ID,isnull(dbo.f_getParent(ID),'') as parentID from BOM
go

--输出结果
/*
ID parentID
----------- ----------------------------------------
1
2 1
3 1,2
4 1,2,3
5 1
*/

--删除测试数据
drop function f_getParent
drop table BOM
go

findee 2009-09-03
  • 打赏
  • 举报
回复
mark,学习.
加载更多回复(3)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧