表是下面的格式产品名称        描述编号          语言          产品描述   apple              1               E             This is an apple.
apple              3               D             Dies ist ein Apfel.
apple              1               F             Il s'agit d'une pomme.
apple              2               E             Apple is good for dinner.
apple              2               D             Apple ist gut für das Abendessen.
........................
每一种产品用5种语言来进行产品描述,其中每种语言的产品描述又被分成了若干段,每段按照 编号 进行排序保存现在我在c#里面想用比较简洁的方式得到每种产品的每种语言的描述,请问应该如何实现?谢谢。

解决方案 »

  1.   

    ----------------------------------------------------------------------------------
    -- Author : htl258(Tony)
    -- Date   : 2010-05-11 15:42:38
    -- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    --          Jul  9 2008 14:43:34 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
    -- Blog   : http://blog.csdn.net/htl258
    ------------------------------------------------------------------------------------> 生成测试数据表: [tb]
    IF OBJECT_ID('[tb]') IS NOT NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb] ([产品名称] [nvarchar](10),[描述编号] [int],[语言] [nvarchar](10),[产品描述] [nvarchar](40))
    INSERT INTO [tb]
    SELECT 'apple','1','E','This is an apple.' UNION ALL
    SELECT 'apple','3','D','Dies ist ein Apfel.' UNION ALL
    SELECT 'apple','1','F','Il sagit dune pomme.' UNION ALL
    SELECT 'apple','2','E','Apple is good for dinner.' UNION ALL
    SELECT 'apple','2','D','Apple ist gut für das Abendessen.'--SELECT * FROM [tb]-->SQL查询如下:
    select distinct 产品名称,语言,
    产品描述=ltrim((select ' '+产品描述 from tb where 产品名称=t.产品名称 and 语言=t.语言 for xml path('')))
    from tb t
    /*
    产品名称       语言         产品描述
    ---------- ---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    apple      D          Dies ist ein Apfel. Apple ist gut für das Abendessen.
    apple      E          This is an apple. Apple is good for dinner.
    apple      F          Il sagit dune pomme.(3 行受影响)
    */
      

  2.   

    ----------------------------------------------------------------------------------
    -- Author : htl258(Tony)
    -- Date   : 2010-05-11 15:42:38
    -- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    --          Jul  9 2008 14:43:34 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
    -- Blog   : http://blog.csdn.net/htl258
    ------------------------------------------------------------------------------------> 生成测试数据表: [tb]
    IF OBJECT_ID('[tb]') IS NOT NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb] ([产品名称] [nvarchar](10),[描述编号] [int],[语言] [nvarchar](10),[产品描述] [nvarchar](40))
    INSERT INTO [tb]
    SELECT 'apple','1','E','This is an apple.' UNION ALL
    SELECT 'apple','3','D','Dies ist ein Apfel.' UNION ALL
    SELECT 'apple','1','F','Il sagit dune pomme.' UNION ALL
    SELECT 'apple','2','E','Apple is good for dinner.' UNION ALL
    SELECT 'apple','2','D','Apple ist gut für das Abendessen.'--SELECT * FROM [tb]-->SQL2000查询如下:
    if object_id('dbo.f_str') is not null drop function dbo.f_str
     go 
    create function dbo.f_str(
    @产品名称 varchar(100),
    @语言 varchar(10)

    returns varchar(8000)
    as
    begin
        declare @str varchar(8000)
        select @str = isnull(@str + ',','') + cast([产品描述] as varchar) 
        from tb 
        where 产品名称=@产品名称 and 语言=@语言
        return @str
    end
    go
    select distinct 产品名称,语言,产品描述=dbo.f_str(产品名称,语言) from tb 
    /*
    产品名称       语言         产品描述
    ---------- ---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    apple      D          Dies ist ein Apfel. Apple ist gut für das Abendessen.
    apple      E          This is an apple. Apple is good for dinner.
    apple      F          Il sagit dune pomme.(3 行受影响)
    */
      

  3.   

    产品名称------描述编号--------语言--------------产品描述 
    apple-----------1--------------E----------------This is an apple.
    apple-----------3--------------D----------------Dies ist ein Apfel.
    apple-----------1--------------F----------------Il s'agit d'une pomme.
    balane----------3--------------E----------------Good for eat
    apple-----------2--------------E----------------Apple is good for dinner.
    apple-----------2--------------D----------------Apple ist gut für das Abendessen.........................
    这个语言是用开头一个字母表示的:
    D  = German
    E  = English
    F  = French
    S  = Spanish我现在想得到每种商品的每种语言的 完整的产品描述
    就是把每种语言的描述1,描述2,描述3。加起来
    得到
    Apple:
    This is an apple.Apple is good for dinner.
    Il s'agit d'une pomme..........
      

  4.   

    3楼高手,非常感谢,我先消化一下,对了,在SQL 2000下也没问题吧
      

  5.   


    tony SF 你用的是什么数据库啊,或者是什么插件,能告诉小弟吗
      

  6.   

    if object_id('dbo.f_str') is not null drop function dbo.f_str
     go 
    alter function dbo.f_str(
    @产品名称 varchar(100),
    @语言 varchar(10)

    returns varchar(8000)
    as
    begin
        declare @str varchar(8000)
        select @str = isnull(@str + ',','') + cast([产品描述] as varchar) 
        from tb 
        where 产品名称=@产品名称 and 语言=@语言
         order by [描述编号]
        return @str
    end
    go
    最好加上一个排序
      

  7.   


    select 产品名称,语言,(select 产品描述 +' '
                          from tb
                          where 产品名称=t.产品名称 and 语言=t.语言 for xml path(''))
    from tb t
    group by t.产品名称,t.语言
      

  8.   

    谢谢htl258及各位大哥,分不够送,下次遇到各位再送,非常感谢
      

  9.   

    下个问题:
    http://topic.csdn.net/u/20100511/17/23d573f6-43ac-4fd7-bf0c-1b0b3d5c566a.html