扁铁折弯护栏:SQL关系显示问题
来源:百度文库 编辑:高考问答 时间:2024/04/29 06:32:48
编号 名称 类别
1001 办公用品 0
1002 铅笔 1001
1003 中性铅笔 1002
如何能得出1003 办公用品/铅笔/中性铅笔
一种自关联实现吧,如:
Select a.myname||'/'||b.myname||'/'||c.myname
From mine a,mine b,mine c
Where b.pcode=a.mycode
and c.pcode = b.mycode;
但这样写就固定了,如果再有更深一层,仍需要改,
反而不如用个函数或过程实现。
而且这种方法实现太死了。如果说有些项目是二层,这样的话就出不来了
谁能帮我写一个存储过程
1001 办公用品 0
1002 铅笔 1001
1003 中性铅笔 1002
如何能得出1003 办公用品/铅笔/中性铅笔
一种自关联实现吧,如:
Select a.myname||'/'||b.myname||'/'||c.myname
From mine a,mine b,mine c
Where b.pcode=a.mycode
and c.pcode = b.mycode;
但这样写就固定了,如果再有更深一层,仍需要改,
反而不如用个函数或过程实现。
而且这种方法实现太死了。如果说有些项目是二层,这样的话就出不来了
谁能帮我写一个存储过程
作一个递归的函数。
string2="1003"
string2=f1("1003")
sub f1(string1)
sql="select 名称,类别
from 表 where 编号='" & string1 & "'"
rs.open sql,conn,1,1
if not rs.eof then
string2=rs("名称") & "/" & string2
if cstr(rs("类别"))="0" then
exit sub
else
f1(rs("类别"))
end if
end if
rs.close
end sub
不大明白意思
我也有同感
select 名称 from 表 where 编号<=1003 order by 编号 desc
写清楚一点
select * from 表单 where 编号="1003"
String str=getString("编号");
String str1=getString("名称");
out.print(str1);
while(str!=0)
{
select * from 表单 where 编号="+str+"
str=getString("编号");
str1=getString("名称");
out.print("/"+str1);
}
行不?呵呵!