--分解字符串包含的信息值去另外一表查询相应的信息
来源:互联网 发布:淘宝裹胸 编辑:程序博客网 时间: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,1) INTO 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 行受影响)
(爱新觉罗.毓华 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,1) INTO 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 行受影响)
- --分解字符串包含的信息值去另外一表查询相应的信息
- 分解字符串包含的信息值后然后合并到另外一表的信息
- messages忽略包含特定字符串的信息
- SQLServer查询所有表所有字段包含xx的信息
- SQLServer查询所有表所有字段包含xx的信息
- 判断字符串是否包含另外的字符串
- 可执行文件的包含信息
- 根据wait_class查询后台的会话包含SQL_TEXT信息;
- Linux下grep查询包含某个关键字的上下文信息
- C++ 处理输入包含特定信息的字符串
- crmsql查询表的信息
- 查询锁表的信息
- 查询oracle表的信息
- mysql查询表的信息
- windows下知道机器名或IP的查询另外一个信息的方法
- /proc/pid下的相应信息说明
- /proc/pid下的相应信息说明
- /proc/pid下的相应信息说明
- 流量控制
- 在.NET中杀死Word,Excel等进程
- 郎咸平:谈产业就业物价
- 修改SOLARIS系统时间
- 把图片转成2进制存到database中
- --分解字符串包含的信息值去另外一表查询相应的信息
- strlen函数
- C#开发木马程序
- C#数据库操作的3种典型用法
- .net ASP下 oledb 连接 经过nat处理的 sqlserver 服务器
- TrimLeft C语言实现
- 600874
- jsp Can't start a cloned connection while in manual transaction mode解决
- 关于ROW_NUMBER()函数