SQL2005,有tb表
  id  name   tIDs
   1   a      1,2,3
   2   b      1,2
TbIDs表
 id    IdName
  1      我
  2      你
  3       他
-------------
想要结果
id  name  idnames
1   a     我,你,他
2   b      我,你

解决方案 »

  1.   

    写个sql函数,把tb表的tIDs字段通过这个函数处理一下,得到IdName
    具体函数就不写了
    分太少
      

  2.   

    能不能把tIDs 字段处理一下啊
      

  3.   

    你可以通过代码处理 也可以通过sql函数
      

  4.   

    select a.*,b.* from tb a,TbIDs b where a.tIDs=b.id;就这样就OK了
      

  5.   

    写个SQL函数返回IdName ,查询中调用这个函数就可以了.
      

  6.   


    --> 测试数据:[tb]
    --> 我的淘宝:http://shop36766744.taobao.com/ if object_id('[tb]') is not null drop table [tb]
    create table [tb]([id] int,[name] varchar(1),[tIDs] varchar(5))
    insert [tb]
    select 1,'a','1,2,3' union all
    select 2,'b','1,2'if object_id('[TbIDs]') is not null drop table [TbIDs]
    create table [TbIDs]([id] int,[IdName] varchar(2))
    insert [TbIDs]
    select 1,'我' union all
    select 2,'你' union all
    select 3,'他'
    create function dbo.f_str(@s varchar(100)) returns varchar(100)
    as
    begin
    select @s=replace(@s,cast(id as varchar(10)),[IdName]) from [TbIDs]
        return @s
    end
    goselect id,name,dbo.f_str([tIDs]) from TB/*
    id          name 
    ----------- ---- ----------------------------------------------------------------------------------------------------
    1           a    我,你,他
    2           b    我,你(2 行受影响)*/drop table [TbIDs]drop table [tb]
      

  7.   

    create table tb(id int, value varchar(10)) 
    insert into tb values(1, 'aa') 
    insert into tb values(1, 'bb') 
    insert into tb values(2, 'aaa') 
    insert into tb values(2, 'bbb') 
    insert into tb values(2, 'ccc') 
    go select id, [values]=stuff((select ','+[value] from tb t where id=tb.id 
    for xml path('')), 1, 1, '') 
    from tb 
    group by id /* 
    id          values 
    ----------- -------------------- 
    1          aa,bb 
    2          aaa,bbb,ccc (2 row(s) affected) 
      

  8.   


    -- 创建建表
    create Table  tb(id int,[name] varchar(10),tIDs varchar(20))
    create Table TbIDs(id int, IdName nvarchar(10))
    insert into tb select 1,'a','1,2,3' 
    union all select 2,'b','1,2'
    insert into  TbIDs select 1,'你'
    union all select 2,'我'
    union all select 3,'他'-- 创建函数
    create   function   [dbo].[f_splitTb](@c   varchar(2000),@split   varchar(2))   
    returns varchar(100)  
    as   
        begin   
        declare @t table(col   varchar(20))   
          while(charindex(@split,@c)<>0)   
            begin   
              insert   @t(col)   values   (substring(@c,1,charindex(@split,@c)-1))   
              set   @c   =   stuff(@c,1,charindex(@split,@c),'')   
            end   
          insert   @t(col)   values   (@c)   
    declare @idName varchar(100)
            SELECT  @idName = isnull(@idName +',','')+convert(varchar(10),T.idName)
    FROM (
    select * from TbIDs 
    where id in (select * from @t)) as T    return @idName 
        end   -- 查询
    select id,[name],dbo.f_splitTb(tb.tIDs,',')
    from tb-- 结果
    1 a 你,我,他
    2 b 你,我