set @p_b = (select fid from w_Storage_Depository where fid in(select fid from dbo.jt_base_company where bj=1))
覃塘网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联建站2013年开创至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站。
改成
select @p_b =fid from w_Storage_Depository where fid in(select fid from dbo.jt_base_company where bj=1)
不用递归
建两个表1省;表2城市,建立视图 关系1对多,表1—〉表2,
select 视图 就可以查出了
应该是这样:
with qry as (select user_id,parent_id from tab where user_id = 32
union all
select tab.user_id,tab.parent_id from tab,qry
where tab.parent_id = qry.id)
select * from qry ;
我用mysql5.0.22,不支持上述语法,oracle就可以(sqlserver应该也可以):
create table tab1(user_id int, parent_id int);
insert into tab1 values(1,null);
insert into tab1 values(32,1);
insert into tab1 values(101,32);
insert into tab1 values(102,32);
insert into tab1 values(201,101);
insert into tab1 values(202,101);
insert into tab1 values(203,102);
insert into tab1 values(204,102);
select * from tab1;
with qry(user_id,parent_id) as (select user_id,parent_id from tab1 where user_id = 32
union all
select tab1.user_id,tab1.parent_id from tab1,qry
where tab1.parent_id = qry.user_id
)
select * from qry;
所以,mysql没有办法了,只有写函数,用循环来实现了。
用标准sql的with实现递归查询(sql2005以上肯定支持,sql2000不清楚是否支持):
with subqry(id,name,pid) as (
select id,name,pid from test1 where id = 5
union all
select test1.id,test1.name,test1.pid from test1,subqry
where test1.pid = subqry.id
)
select * from subqry;
with district as ( select * from SECFUNMODULE where MENUID='030' union all select a.* from SECFUNMODULE a, district b where a.PMENUID = b.MENUID ), district1 as ( select a.* from district a where a.MENUID in (select PMENUID from district) ) select * from district1 order by MENUID with当然可以实现. 请参照sqlserver2005 的递归查询
在SQL
SERVER
2000
中你可以先一些自定义函数,或一些存储过程,实现递归:
select
level,TypeName
from
ProductType
t
START
WITH
t.ParentID=0
CONNECT
BY
PRIOR
t.ProductTypeID=
t.ParentID;