有一个公司表
comid=公司ID,
comname=公司name,
还有个要命的bussinessRange=公司所属类一个类别表
classid=类ID名,classname=类名,rootid=类的根ID说明一下,bussinessRange是以选中的类的集合
以空格分隔,不是以,分隔
rootid简单地说,就是某个类他祖宗是哪个类,复杂地说就是他所在的类的顶层类现在,有个一个查询,给出个条件,顶层类的ID,查所有,bussinessRange字段中有子类是这个顶层类的子孙的的记录,统计出来难度出在,
一,bussinessRange以,分隔的
二,套了好几层
三,最好是一个and(......),能解决的因为,我现在是select (select .....) as company total
要变动要改好几块地方,哎怎么办
comid=公司ID,
comname=公司name,
还有个要命的bussinessRange=公司所属类一个类别表
classid=类ID名,classname=类名,rootid=类的根ID说明一下,bussinessRange是以选中的类的集合
以空格分隔,不是以,分隔
rootid简单地说,就是某个类他祖宗是哪个类,复杂地说就是他所在的类的顶层类现在,有个一个查询,给出个条件,顶层类的ID,查所有,bussinessRange字段中有子类是这个顶层类的子孙的的记录,统计出来难度出在,
一,bussinessRange以,分隔的
二,套了好几层
三,最好是一个and(......),能解决的因为,我现在是select (select .....) as company total
要变动要改好几块地方,哎怎么办
难度出在,
一,bussinessRange不以,分隔的,而是以空格分隔的
bussinessRange为什么是类的集合?罗嗦,浪费服务器资源。
@str1 as varchar(2000),
@str2 as varchar(2000)
)
returns int
as
begin declare @t1 table (s varchar(30))
while charindex(' ',@str1)>0
begin
insert @t1 values(left(@str1,charindex(' ',@str1)-1))
set @str1=right(@str1,len(@str1)-charindex(' ',@str1))
end
insert @t1 values(@str1) while charindex(' ',@str2)>0
begin
if exists (select 1 from @t1 where s=left(@str2,charindex(' ',@str2)-1))
return 1
set @str2=right(@str2,len(@str2)-charindex(' ',@str2))
end
if exists (select 1 from @t1 where s=@str2)
return 1
return 0
endGO--如果是classid+空格+classiddeclare @s varchar(2000)
set @s=''
select @s=@s+' '+cast(classid as varchar) from 类别表 where rootid=给出的顶层类ID
--查询
select count(*) from 公司表
where dbo.fn_in(bussinessRange,@s)=1--如果是classname+空格+classnamedeclare @s varchar(2000)
set @s=''
select @s=@s+' '+classname from 类别表 where rootid=给出的顶层类ID
--查询
select count(*) from 公司表
where dbo.fn_in(bussinessRange,@s)=1
还是 classname+空格+classname
classid classname rootid
1 大类1 1
2 大类2 2
3 小类1 1
4 小类2 1
5 小类3 1
6
7
8comany表
comid comname bussinessRange
1 XX公司 3,7,8(现在可以用,号隔,但好像用处也不大)现在选择大类1
大类1的ID是1
所以XX公司就被查到了
因为bussinessRange里有,3这个类号
而3类号的顶层是1
所以被查到,
...................................
就是这样
bussinessRange为什么是类的集合?罗嗦,浪费服务器资源。
请教,怎么剪,谢谢啦