--分解字符串包含的信息值去另外一表查询相应的信息

来源:互联网 发布:淘宝裹胸 编辑:程序博客网 时间:2024/04/28 18:11

 

--分解字符串包含的信息值去另外一表查询相应的信息
(爱新觉罗.毓华  2007-12-22  广东深圳)

/*问题描述:
需要将用户表中的用户组与信息表中的用户组对比.如果有相同的组则可查看该信息. 
一个用户可能是几个用户组的成员.一条信息可能是几个用户组都可以查看. 

表一 
ID     usergroup     username 
1      1,2,4         用户A 
2      3             用户B 

表二 
id     usergroup     title   
1      1,3,4         信息标题1 
2      3             信息标题2 
3      1,2,3         信息标题3 

用户A可以看到   
表二中的 
信息标题1 
信息标题3 

用户B可以看到   
表二中的 
信息标题1 
信息标题2 
信息标题3
*/


-------------------------------------------------------------
--
sql server 2000中用临时表的写法.
create table TB(ID int,usergroup varchar(20),username varchar(10))
insert into TB values(1,'1,2,4','用户A'
insert into TB values(2,'3'    ,'用户B')
create table TA(id int,usergroup varchar(20),title varchar(10))
insert into TA values(1,'1,3,4','信息标题1'
insert into TA values(2,'3'    ,'信息标题2'
insert into TA values(3,'1,2,3','信息标题3'
go

--建立一个辅助的临时表就可以了
SELECT TOP 8000 id = identity(int,1,1INTO tmp FROM syscolumns a, syscolumns b  
 
select distinct c.username , d.title from
(
  
SELECT A.username,usergroup = SUBSTRING(A.usergroup, B.ID, CHARINDEX(',', A.usergroup + ',', B.ID) - B.ID) FROM tb A, tmp B WHERE SUBSTRING(',' + a.usergroup, B.id, 1= ',' 
) c, TA d
where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ','> 0
order by username , title

drop table TB,TA,tmp

/*
username   title
---------- ----------
用户A        信息标题1
用户A        信息标题3
用户B        信息标题1
用户B        信息标题2
用户B        信息标题3

(5 行受影响)
*/


--------------------------------------------------------------------
--
sql server 2000中不用临时表的SQL语句.
create table TB(ID int,usergroup varchar(20),username varchar(10))
insert into TB values(1,'1,2,4','用户A'
insert into TB values(2,'3'    ,'用户B')
create table TA(id int,usergroup varchar(20),title varchar(10))
insert into TA values(1,'1,3,4','信息标题1'
insert into TA values(2,'3'    ,'信息标题2'
insert into TA values(3,'1,2,3','信息标题3'
go

select distinct c.username , d.title from
(
  
select a.username,usergroup=substring(a.usergroup,b.id,charindex(',',a.usergroup+',',b.id)-b.id)
  
from tb a,(select 1 as id union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9  union select 10 union select 11 union select 12 union select 13 union select 14 union select 15) b
  
where substring(','+a.usergroup,b.id,1)=','
) c, TA d
where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ','> 0
order by username , title

drop table TB,TA

/*
username   title
---------- ----------
用户A        信息标题1
用户A        信息标题3
用户B        信息标题1
用户B        信息标题2
用户B        信息标题3

(5 行受影响)
*/


-------------------------------------------------------------------
--
sql server 2005中的写法.
create table TB(ID int,usergroup varchar(20),username varchar(10))
insert into TB values(1,'1,2,4','用户A'
insert into TB values(2,'3'    ,'用户B')
create table TA(id int,usergroup varchar(20),title varchar(10))
insert into TA values(1,'1,3,4','信息标题1'
insert into TA values(2,'3'    ,'信息标题2'
insert into TA values(3,'1,2,3','信息标题3'
go

select distinct c.username , d.title from
(
  
SELECT A.username, B.usergroup FROM(SELECT username, [usergroup] = CONVERT(xml,'<root><v>' + REPLACE([usergroup]',''</v><v>'+ '</v></root>'FROM TB)A
  
OUTER APPLY(SELECT usergroup = N.v.value('.''varchar(100)'FROM A.[usergroup].nodes('/root/v') N(v))B
) c, TA d
where charindex(',' + c.usergroup + ',' , ',' + d.usergroup + ','> 0
order by username , title

drop table TB,TA

/*
username   title
---------- ----------
用户A        信息标题1
用户A        信息标题3
用户B        信息标题1
用户B        信息标题2
用户B        信息标题3

(5 行受影响)

原创粉丝点击