ID        A         
1 a,b,c   
2 b,c,a   
3 c,b,a   
4        aa,bb,cc    
5        bb,aa,cc      
6        cc,bb,aa    
7        n,b,h现在我要查询出来的结果 这样1      a,b,c
4      aa,bb,cc
7      n,b,h

解决方案 »

  1.   

    select * from tb where id in(1,4,7)
      

  2.   

    select * from tb where id in(1,4,7)
      

  3.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-10 10:20:28
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([ID] int,[A] varchar(8))
    insert [tb]
    select 1,'a,b,c' union all
    select 2,'b,c,a' union all
    select 3,'c,b,a' union all
    select 4,'aa,bb,cc' union all
    select 5,'bb,aa,cc' union all
    select 6,'cc,bb,aa' union all
    select 7,'n,b,h'
    --------------开始查询--------------------------select * from [tb] where [id] in(1,4,7)
    ----------------结果----------------------------
    /*
    ID          A        
    ----------- -------- 
    1           a,b,c
    4           aa,bb,cc
    7           n,b,h(所影响的行数为 3 行)*/
      

  4.   

    select * from tb where id in(1,4,7)????
      

  5.   

    1、先拆分。
    分拆列值 有表tb, 如下: 
    id          value 
    ----------- ----------- 
    1          aa,bb 
    2          aaa,bbb,ccc 
    欲按id,分拆value列, 分拆后结果如下: 
    id          value 
    ----------- -------- 
    1          aa 
    1          bb 
    2          aaa 
    2          bbb 
    2          ccc 1. 旧的解决方法(sql server 2000) 
     
    select a.id, substring(a.[value], b.number, charindex(',', a.[value] + ',', b.number) - b.number) 
    FROM tb a, master..spt_values  b 
    WHERE b.type='p' and substring(',' + a.[value],b.number, 1) = ',' 
    2. 新的解决方法(sql server 2005) create table tb(id int,value varchar(30)) 
    insert into tb values(1,'aa,bb') 
    insert into tb values(2,'aaa,bbb,ccc') 
    go 
    SELECT a.id, b.value 
    FROM( 
        SELECT id, [value] = CONVERT(xml,' <root> <v>' + REPLaCE([value], ',', ' </v> <v>') + ' </v> </root>') FROM tb 
    )a 
    OUTER aPPLY( 
        SELECT value = N.v.value('.', 'varchar(100)') FROM a.[value].nodes('/root/v') N(v) 
    )b DROP TabLE tb /* 
    id          value 
    ----------- ------------------------------ 
    1          aa 
    1          bb 
    2          aaa 
    2          bbb 
    2          ccc (5 行受影响) 
    */
      

  6.   

    select * from tb where id in(1,4,7)
      

  7.   

    2、再按顺序排序合并。
    --******************************************************************
    --                       合并列值 
    --******************************************************************
    表结构,数据如下: 
    id    value 
    ----- ------ 
    1    aa 
    1    bb 
    2    aaa 
    2    bbb 
    2    ccc 需要得到结果: 
    id    values 
    ------ ----------- 
    1      aa,bb 
    2      aaa,bbb,ccc 
    即:group by id, 求 value 的和(字符串相加) 1. 旧的解决方法(在sql server 2000中只能用函数解决。) 
    --===================================================================
    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 
    --1. 创建处理函数 
    CREATE FUNCTION dbo.f_strUnite(@id int) 
    RETURNS varchar(8000) 
    AS 
    BEGIN 
        DECLARE @str varchar(8000) 
        SET @str = '' 
        SELECT @str = @str + ',' + value FROM tb WHERE id=@id 
        RETURN STUFF(@str, 1, 1, '') 
    END 
    GO 
    -- 调用函数 
    SELECt id, value = dbo.f_strUnite(id) FROM tb GROUP BY id 
    drop table tb 
    drop function dbo.f_strUnite 
    go
    /* 
    id          value      
    ----------- ----------- 
    1          aa,bb 
    2          aaa,bbb,ccc 
    (所影响的行数为 2 行) 
    */ 
    --===================================================================================
    2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。) 
    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 * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY( 
            SELECT [values]= STUFF(REPLACE(REPLACE( 
                ( 
                    SELECT value FROM tb N 
                    WHERE id = A.id 
                    FOR XML AUTO 
                ), ' <N value="', ','), '"/>', ''), 1, 1, '') 
    )N 
    drop table tb /* 
    id          values 
    ----------- ----------- 
    1          aa,bb 
    2          aaa,bbb,ccc (2 行受影响) 
    */ --SQL2005中的方法2 
    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) */ drop table tb 
      

  8.   

    --> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([ID] int,[A] nvarchar(8))
    Insert tb
    Select 1,'a,b,c' union all
    Select 2,'b,c,a' union all
    Select 3,'c,b,a' union all
    Select 4,'aa,bb,cc' union all
    Select 5,'bb,aa,cc' union all
    Select 6,'cc,bb,aa' union all
    Select 7,'n,b,h'
    Go
    --Select * from tb-->SQL查询如下:
    ;with t as 
    (
     select 
        a.id,
        a=substring(a.a, b.number, charindex(',', a.a + ',', b.number) - b.number)
      from tb a,master..spt_values b 
      where b.number>0 
        and b.type='p' 
        and substring(',' + a.a,b.number,1) = ',' 
    ),t1 as
    (
      select distinct id,
    stuff((select ','+a from t where id=a.id order by a for xml path('')),1,1,'') a
      from t a
    )
    select * 
    from t1 a 
    where not exists(
    select 1
    from t1
    where a=a.a
    and id<a.id)
    /*
    id          a
    ----------- ----------------
    1           a,b,c
    4           aa,bb,cc
    7           b,h,n(3 行受影响)*/
      

  9.   

    --> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([ID] int,[A] nvarchar(8))
    Insert tb
    Select 1,'a,b,c' union all
    Select 2,'b,c,a' union all
    Select 3,'c,b,a' union all
    Select 4,'aa,bb,cc' union all
    Select 5,'bb,aa,cc' union all
    Select 6,'cc,bb,aa' union all
    Select 7,'n,b,h'
    Go
    --Select * from tb-->SQL查询如下:
    ;with t as 
    (
     select 
        a.id,
        a=substring(a.a, b.number, charindex(',', a.a + ',', b.number) - b.number)
      from tb a,master..spt_values b 
      where b.number>0 
        and b.type='p' 
        and substring(',' + a.a,b.number,1) = ',' 
    ),t1 as
    (
      select distinct id,
    stuff((select ','+a from t where id=a.id order by a for xml path('')),1,1,'') a
      from t a
    )
    select * 
    from tb 
    where exists(
    select * 
    from t1 a 
    where not exists(
    select 1
    from t1
    where a=a.a
    and id<a.id)
    and tb.id=a.id)
    /*
    ID          A
    ----------- --------
    1           a,b,c
    4           aa,bb,cc
    7           n,b,h(3 行受影响)*/这样才是对的.