table1id  content
1   2月
2   3月
3   4月
4   5月
怎么把id1到4的content合并成 2月3月4月 一个单元格?

解决方案 »

  1.   

    table1
    id  content 
    1  2月 
    2  3月 
    3  4月 
    4  5月 
    一条sql查询出id=1,2,3,4 时的content ,就是把content都合并起来
      

  2.   

    --> 测试数据:@tb
    declare @tb table([id] int,[content] varchar(3))
    insert @tb
    select 1,'2月' union all
    select 2,'3月' union all
    select 3,'4月' union all
    select 4,'5月'declare @s varchar(100)
    select @s=isnull(@s,'')+[content] from @tb where id>=1 and id<4
    select @s
    /*
    ----------------------------------------------------------------------------------------------------
    2月3月4月(1 行受影响)*/
      

  3.   

    DECLARE @STR VARCHAR(8000)SELECT @STR=ISNULL(@STR+',','')+name FROM (SELECT DISTINCT content FROM A)AS T
        where  id>1 and id <4SELECT @STR
      

  4.   

    ---考虑可能出现重复 加个distinct
    DECLARE @STR VARCHAR(8000)SELECT @STR=ISNULL(@STR+',','')+[content] FROM (SELECT DISTINCT content FROM A)AS T
        where  id>1 and id <4SELECT @STR