create table #A(a1 varchar(100),a2 varchar(100),a3 varchar(100))
insert #a values('name','10,20,15','a,b,c')
create table #b (B1 int,B2 varchar(100))
insert #b values(10,    '10values')
insert #b values(20 ,   '20values')
insert #b values(15  ,  '15values')
create table #c(c1 varchar(10),c2 varchar(100))
insert #c values('a','avalues')
insert #c values('b','bvalues')
insert #c values('c','cvalues')select top 8000 identity(int,1,1) id into #dali from sysobjects a,sysobjects b
select a1,substring(','+a2,id+1,charindex(',',','+a2+',',id+1)-id-1) b1,0 flag into #1 from #a aa,#dali bb where substring(','+a2,id,8000) like ',_%'select a1,substring(','+a3,id+1,charindex(',',','+a3+',',id+1)-id-1) c1,0 flag into #2 from #a aa,#dali bb where substring(','+a3,id,8000) like ',_%'declare @a int,@b varchar(100)
update #1 set @a=case when @b=a1 then @a+1 else 1 end,@b=a1,flag=@a
set @b=null
update #2 set @a=case when @b=a1 then @a+1 else 1 end,@b=a1,flag=@aselect a1.a1,b.b1,c.c1 from #1 a1 join #2 a2 on a1.a1=a2.a1 and a1.flag=a2.flag join #b b on a1.b1=b.b1 join #c c on a2.c1=c.c1godrop table #a,#b,#c,#dali ,#1,#2

解决方案 »

  1.   

    #a,#b,#c为你的表,其他为临时表
      

  2.   

    谢谢大力
    如果我将表C去掉
    表A字段a3为1,2,3
    就是说字段a3是和字段a2位置对应的相应的数字,该如何实现呢
      

  3.   

    我已经考虑到了,你只要小改一下,就可以了。create table #A(a1 varchar(100),a2 varchar(100),a3 varchar(100))
    insert #a values('name','10,20,15','a,b')
    create table #b (B1 int,B2 varchar(100))
    insert #b values(10,    '10values')
    insert #b values(20 ,   '20values')
    insert #b values(15  ,  '15values')
    create table #c(c1 varchar(10),c2 varchar(100))
    insert #c values('a','avalues')
    insert #c values('b','bvalues')
    insert #c values('c','cvalues')select top 8000 identity(int,1,1) id into #dali from sysobjects a,sysobjects b
    select a1,substring(','+a2,id+1,charindex(',',','+a2+',',id+1)-id-1) b1,0 flag into #1 from #a aa,#dali bb where substring(','+a2,id,8000) like ',_%'select a1,substring(','+a3,id+1,charindex(',',','+a3+',',id+1)-id-1) c1,0 flag into #2 from #a aa,#dali bb where substring(','+a3,id,8000) like ',_%'declare @a int,@b varchar(100)
    update #1 set @a=case when @b=a1 then @a+1 else 1 end,@b=a1,flag=@a
    set @b=null
    update #2 set @a=case when @b=a1 then @a+1 else 1 end,@b=a1,flag=@aselect isnull(a1.a1,a2.a1) a1,b.b1,c.c1 from #1 a1 full join #2 a2 on a1.a1=a2.a1 and a1.flag=a2.flag left join #b b on a1.b1=b.b1 left join #c c on a2.c1=c.c1godrop table #a,#b,#c,#dali ,#1,#2
      

  4.   

    关键在数据分拆上:
    --创建数据测试环境(楼主提供的原表)
    declare @A table(a1 varchar(100),a2 varchar(100),a3 varchar(100))
    insert @A values('name','10,20,15','a,b,c')declare @B table(b1 int,b2 varchar(100))
    insert @B
    select 10,'10values'
    union all select 20,'20values'
    union all select 15,'15values'declare @C table(c1 varchar(10),c2 varchar(100))
    insert into @C
    select  'a','avalues'
    union all select 'b','bvalues'
    union all select 'c','cvalues'/*以下是数据处理部分*/
    --创建数据分拆临时表
    select top 100 id=identity(int,1,1) into #tb
    from
    (select top 100 id from syscolumns) a
    ,(select top 100 id from syscolumns) b
    ,(select top 100 id from syscolumns) c/*数据分拆,将列表分拆成多列*/
    --分拆a2列,并生成临时表#tba
    select id=identity(int,1,1),a1
    ,a2=substring(a2,b.id,charindex(',',a2+',',b.id)-b.id)
    into #tba
    from @A a,#tb b
    where substring(','+a2,b.id,1)=','--分拆a3列,并生成临时表#tbb
    select id=identity(int,1,1),a1
    ,a3=substring(a3,b.id,charindex(',',a3+',',b.id)-b.id)
    into #tbb
    from @A a,#tb b
    where substring(','+a3,b.id,1)=',' --根据分拆的结果来生成查询结果.
    select a.a1,b2,c2
    from(
    select a.id,a.a1,b.b2 from #tba a,@B b where a.a2=b.b1
    ) a inner join (
    select a.id,a.a1,c.c2 from #tbb a,@C c where a.a3=c.c1
    ) b on a.id=b.id--删除数据处理中生成的临时表
    drop table #tb,#tba,#tbb
      

  5.   

    --创建数据测试环境(楼主的原表)
    declare @A table(a1 varchar(100),a2 varchar(100),a3 varchar(100))
    insert @A values('name','10,20,15','a,b,c')declare @B table(b1 int,b2 varchar(100))
    insert @B
    select 10,'10values'
    union all select 20,'20values'
    union all select 15,'15values'declare @C table(c1 varchar(10),c2 varchar(100))
    insert into @C
    select  'a','avalues'
    union all select 'b','bvalues'
    union all select 'c','cvalues'/*以下是数据处理部分*/
    --创建字段列分拆临时表
    select top 100 id=identity(int,1,1) into #tb
    from
    (select top 100 id from syscolumns) a
    ,(select top 100 id from syscolumns) b
    ,(select top 100 id from syscolumns) c--字段列数据分拆
    --分拆a2列,生成临时表#tba
    select id=identity(int,1,1),a1
    ,a2=substring(a2,b.id,charindex(',',a2+',',b.id)-b.id)
    into #tba
    from @A a,#tb b
    where substring(','+a2,b.id,1)=','--分拆a3列,生成临时表#tbb
    select id=identity(int,1,1),a1
    ,a3=substring(a3,b.id,charindex(',',a3+',',b.id)-b.id)
    into #tbb
    from @A a,#tb b
    where substring(','+a3,b.id,1)=',' --得到结果
    select a.a1,b2,c2
    from(
    select a.id,a.a1,b.b2 from #tba a,@B b where a.a2=b.b1
    ) a inner join (
    select a.id,a.a1,c.c2 from #tbb a,@C c where a.a3=c.c1
    ) b on a.id=b.id--删除临时表
    drop table #tb,#tba,#tbb
      

  6.   

    select * from 
    a left join b ON chindex(','+cast(b1 as varchar)+',' , ','+a2+',')>0
    left jion c on  chindex(','+c1+',' , ','+a3+',')>0
      

  7.   

    select *,IDENTITY(int, 1, 1) as s into #d from b
    select *,IDENTITY(int, 1, 1) as s into #e from cselect * from 
    (
    select *  from 
    a right join #d on charindex(','+convert(varchar(20),b1)+',' , ','+a2+',')>0) a
    inner join (
    select * from 
    a right join #e on charindex(','+c1+',' , ','+a3+',')>0
    ) b 
    on  a.s = b.s