34,590
社区成员
发帖
与我相关
我的任务
分享
Select distinct(C.ZID),C.ZNAME,A.UID from a left join b on a.uid=b.uid left join D on c.UID=D.UID left join C on C.ZID=D.ZID
create table users(id varchar(8),zw varchar(20))
insert into users values('001','1,2,5')
insert into users values('002','2,4' )
create table sources(id int,qx varchar(20))
insert into sources values(1,'1,3')
insert into sources values(2,'2,3')
insert into sources values(3,'1,5')
go
create function f_include(@zw varchar(20),@qx varchar(20))
returns bit
as
begin
declare @str varchar(10)
select @zw=@zw+',',@qx=','+@qx+','
while charindex(',',@zw)>0
begin
select @str=','+left(@zw,charindex(',',@zw)),
@zw =stuff(@zw,1,charindex(',',@zw),'')
if charindex(@str,@qx)>0
return 1
end
return 0
end
go
select u.*,s.* from users u,sources s where dbo.f_include(u.zw,s.qx)=1
/*
id zw id qx
-------- -------------------- ----------- --------------------
001 1,2,5 1 1,3
001 1,2,5 2 2,3
002 2,4 2 2,3
001 1,2,5 3 1,5
*/
go
drop function f_include
drop table users,sources
go
Create table aa(Id varchar(10),zw varchar(10))
insert into aa select '001','1,2,5'
insert into aa select '002','2,4'
create table bb(Id int,qx varchar(10))
insert into bb select 1,'1,3'
insert into bb select 2,'2,3'
insert into bb select 3,'1,5'
select id=Identity(int,1,1) into # from sysobjects
select a.id,b.id as qx from
(
select a.id,zw=substring(a.zw,b.id,charindex(',',a.zw+',',b.id)-b.id) from aa a,# b
where substring(','+a.zw,b.id,1)=','
) a, bb b
where charindex(','+a.zw+',',','+b.qx+',')>0
group by a.id,b.id
/*
id qx
---------- -----------
001 1
001 2
001 3
002 2
(4 行受影响)
*/