求select语句 大家都来看看啊。

sky266 2006-08-12 02:41:09
一分类表 a
三个字段 id name parentid

另一张内容表 b

里面一个字段C会有多个 a 表的id,以逗号隔开。 父子类都可能有,但是父子id不会出现在同一个字段里面。
需要查询出来b表中与a标id相关的行。如果是父id 则它的子id也要查出来。
...全文
128 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky266 2006-08-12
  • 打赏
  • 举报
回复
传进来的参数应该是 c 字段相关的一个字符串。
sky266 2006-08-12
  • 打赏
  • 举报
回复
感谢pao1uo(冒牌游泳的鱼 V0.1)。
不过我的这个需要根据外面传进来的值 查询出来C字段所在行的id的。
你的函数是不是把id当成参数传进去的。
zsforever 2006-08-12
  • 打赏
  • 举报
回复
给一个参考方案:
create table aa(id varchar(8) ,name varchar(16),parentid varchar(8))
create table bb(sid varchar(64))
insert into aa select
'1', '中国', '0' union all select
'2', '湖南', '1' union all select
'3', '河南', '1' union all select
'4', '北京', '1' union all select
'5', '美国', '0' union all select
'6', '纽约', '5'
insert into bb select
'2,3' union all select
'2,4' union all select
'3,4' union all select
'5'
CREATE FUNCTION [dbo].[A_id] (@id varchar(64))
RETURNS varchar(128) AS
BEGIN
declare @l_id varchar(64),@A_id varchar(128)
set @l_id = @id+','
set @A_id = ''
while charindex(',',@l_id) > 0
begin
declare @f_id varchar(8)
set @f_id = left(@l_id,charindex(',',@l_id)-1)
set @A_id = @A_id + '[' + @f_id + ']'
while exists(select 1 from aa where id=@f_id and parentid <> 0)
begin
select @f_id = parentid from aa where id=@f_id and parentid <> 0
set @A_id = @A_id + '[' + @f_id + ']'
end
set @l_id = right(@l_id,len(@l_id)-charindex(',',@l_id))
end
return @A_id
END
go

declare @q_id varchar(32)
set @q_id = '2'
select sid from bb where charindex('['+@q_id+']',dbo.A_id(sid)) > 0
sid
----------------------------------------------------------------
2,3
2,4

(所影响的行数为 2 行)


pao1uo 2006-08-12
  • 打赏
  • 举报
回复
写到一个函数了,自己慢慢研究吧

表结构最好给齐了

--建立环境
create table a(
id int,
name varchar(10),
parentid int
)

create table b(
id int,
C varchar(20)
)


--插入数据
insert a
select 1,'1',0
union all select
2,'2',0
union all select
3,'3',0
union all select
11,'11',1
union all select
12,'12',1
union all select
13,'13',1
union all select
21,'21',2
union all select
22,'22',2
union all select
111,'111',11
union all select
112,'112',11
union all select
131,'131',13
union all select
211,'211',21


insert b
select 1,'13,111,211'
union all
select 2,'3,11,13'
union all
select 3,'1,2'

--建立函数
create function fn_ab(
@bid int
)
returns @r table (
bid int,
aid int,
name varchar(10),
parentid int
)
as
begin
--拆分id
declare @c varchar(20)
declare @t table (
id int
)
select @c=c from b where id=@bid
if isnull(@c,'')='' return
declare @i int
set @i=charindex(',',@c)
while @i>0
begin
insert @t values(left(@c,@i-1))
set @c=stuff(@c,1,@i,'')
set @i=charindex(',',@c)
end
if @c<>'' insert @t values(@c)
--初步结果
insert @r select @bid,* from a where id in (select id from @t)
--取子id
while exists (select 1
from a
where parentid in (select aid from @r)
and id not in (select aid from @r)
)
insert @r select @bid,*
from a
where parentid in (select aid from @r)
and id not in (select aid from @r)
return
end

go

--调用
select * from dbo.fn_ab(1)
--结果
bid aid name parentid
----------- ----------- ---------- -----------
1 13 13 1
1 111 111 11
1 211 211 21
1 131 131 13

(所影响的行数为 4 行)

--调用
select * from dbo.fn_ab(2)
--结果
bid aid name parentid
----------- ----------- ---------- -----------
2 3 3 0
2 11 11 1
2 13 13 1
2 111 111 11
2 112 112 11
2 131 131 13

(所影响的行数为 6 行)


--调用
select * from dbo.fn_ab(3)
--结果
bid aid name parentid
----------- ----------- ---------- -----------
3 1 1 0
3 2 2 0
3 11 11 1
3 12 12 1
3 13 13 1
3 21 21 2
3 22 22 2
3 111 111 11
3 112 112 11
3 131 131 13
3 211 211 21

(所影响的行数为 11 行)

sky266 2006-08-12
  • 打赏
  • 举报
回复
表a
1 中国 0
2 湖南 1
3 河南 1
4 北京 1
5 美国 0
6 纽约 5
表B 的字段C
2,3
2,4
3,4
5
如果查询条件是 C字段=1
那么需要查出来B表的1、2、3条
如果查询条件是C字段=2
那么查询出来1、2条
如果是5 查询出来第4条
问题还如何加分啊。没有估计到问题的难度。
九斤半 2006-08-12
  • 打赏
  • 举报
回复
表A用哪个字段确定子类的父类ID?
mugua604 2006-08-12
  • 打赏
  • 举报
回复
给出表结构.........
pao1uo 2006-08-12
  • 打赏
  • 举报
回复
要好多函数哦

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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