有下面两个表
客户表
客户id  客户名称name 
1       Allen
2       Bill
3       Candy
4       David客户购买物品表
id   物品id
1    2
1    3
2    1
2    4
3    1
3    2
3    3我想生成下面的表
客户ID 客户名字  客户购买物品
1      Allen     2,3
2      Bill      1,4
3      Candy     1,2,3
4      David即有一列 显示用逗号分割的 客户购买的物品应该怎么整,非常感谢各位大侠。

解决方案 »

  1.   

    CREATE FUNCTION dbo.f_str(@id int)
    RETURNS varchar(8000)
    AS
    BEGIN
      DECLARE @r varchar(8000)
      SET @r = ''
      SELECT @r = @r + ',' + value FROM tb WHERE id=@id
      RETURN STUFF(@r, 1, 1, '')
    END
    GO-- 调用函数
    SELECt id, value = dbo.f_str(id) FROM tb GROUP BY iddrop table tb
    drop function dbo.f_str
      

  2.   


    --> 测试数据:[tba]
    if object_id('[tba]') is not null 
    drop table [tba]
    create table [tba](
    [id] int,
    [name] varchar(5)
    )
    insert [tba]
    select 1,'Allen' union all
    select 2,'Bill' union all
    select 3,'Candy' union all
    select 4,'David'
    --> 测试数据:[tbb]
    if object_id('[tbb]') is not null 
    drop table [tbb]
    create table [tbb](
    [id] int,
    [pid] int
    )
    insert [tbb]
    select 1,2 union all
    select 1,3 union all
    select 2,1 union all
    select 2,4 union all
    select 3,1 union all
    select 3,2 union all
    select 3,3with t
    as(
    select 
        a.id,
        a.name,
        b.pid 
    from 
        tbb b
    inner join 
        tba a
    on 
        a.id=b.id
    )
    SELECT 
        a.id,
        a.name,
        GoodsId=STUFF((SELECT ','+LTRIM(b.pid) 
    FROM 
        t  b
    WHERE 
        a.id=b.id
        FOR XML PATH('')),1,1,'')
        FROM 
            t  a
    GROUP BY
         a.id,a.name
    /*
    id name GoodsId
    ------------------------------
    1 Allen 2,3
    2 Bill 1,4
    3 Candy 1,2,3
    */
      

  3.   

    --> 测试数据:[tba]
    if object_id('[tba]') is not null 
    drop table [tba]
    create table [tba](
    [id] int,
    [name] varchar(5)
    )
    insert [tba]
    select 1,'Allen' union all
    select 2,'Bill' union all
    select 3,'Candy' union all
    select 4,'David'
    --> 测试数据:[tbb]
    if object_id('[tbb]') is not null 
    drop table [tbb]
    create table [tbb](
    [id] int,
    [pid] int
    )
    insert [tbb]
    select 1,2 union all
    select 1,3 union all
    select 2,1 union all
    select 2,4 union all
    select 3,1 union all
    select 3,2 union all
    select 3,3with t
    as(
    select 
        a.id,
        a.name,
        b.pid 
    from 
        tbb b
    right join 
        tba a
    on 
        a.id=b.id
    )
    SELECT 
        a.id,
        a.name,
        GoodsId=isnull(STUFF((SELECT ','+LTRIM(b.pid) 
    FROM 
        t  b
    WHERE 
        a.id=b.id
        FOR XML PATH('')),1,1,''),'')
        FROM 
            t  a
    GROUP BY
         a.id,a.name
    /*
    id name GoodsId
    -----------------------------
    1 Allen 2,3
    2 Bill 1,4
    3 Candy 1,2,3
    4 David
    */--更改一下
      

  4.   

    --> 测试数据:[客户表]
    IF OBJECT_ID('[客户表]') IS NOT NULL 
       DROP TABLE [客户表]
    GO 
    CREATE TABLE [客户表]
           (
             [客户id] INT ,
             [客户名称name] VARCHAR(5)
           )
    INSERT  [客户表]
            SELECT  1, 'Allen'
            UNION ALL
            SELECT  2, 'Bill'
            UNION ALL
            SELECT  3, 'Candy'
            UNION ALL
            SELECT  4, 'David'--> 测试数据:[客户购买物品表]
    IF OBJECT_ID('[客户购买物品表]') IS NOT NULL 
       DROP TABLE [客户购买物品表]
    GO 
    CREATE TABLE [客户购买物品表] ( [id] INT, [物品id] INT )
    INSERT  [客户购买物品表]
            SELECT  1, 2
            UNION ALL
            SELECT  1, 3
            UNION ALL
            SELECT  2, 1
            UNION ALL
            SELECT  2, 4
            UNION ALL
            SELECT  3, 1
            UNION ALL
            SELECT  3, 2
            UNION ALL
            SELECT  3, 3
    --------------开始查询--------------------------SELECT  a.[客户id], a.[客户名称name], STUFF((
                                            SELECT ','+ LTRIM ([物品id]) FROM [客户购买物品表] WHERE [id]= b.[id] 
                                          FOR
                                            XML PATH('')
                                          ), 1, 1, '')
    FROM    [客户表] AS a
    LEFT JOIN [客户购买物品表] AS b
    ON      a.[客户id] = b.[id]
    GROUP BY a.[客户id], a.[客户名称name], b.[id]
    ----------------结果----------------------------
    /* 
    客户id        客户名称name 
    ----------- -------- ------------
    1           Allen    2,3
    2           Bill     1,4
    3           Candy    1,2,3
    4           David    NULL
    */
      

  5.   

    实在不好意思。
    发错了,我想在mysql里用。咋整啊,上面两个大侠写的,真心不懂啊。