zoukankan      html  css  js  c++  java
  • 在数据库中将字表中的多行合并到一列中

    几年前就做过这样的查询,在最近的项目中又遇到这样的需求,在此记录一下。

    假设有一个文章表Post和一个评论表Comments,可以对文章进行多次评论,现在希望在对Post表查询时能将Post的所有评论内容组合到一个字段中显示。

    首先创建表Post和Comments

    create table Post
    (
        [PostID] int identity(1,1) primary key not null,
        [Title] nvarchar(50),
        [Content] text,
        [CreateDate] datetime default getdate()    
    )
    
    create table Comments
    (
        [CommentID] int identity(1,1) primary key not null,
        [PostID] int,
        [Content] text,
        [CreateDate] datetime default getdate()
    )

    给这两个表添加一些测试数据

    insert into Post select '钓鱼岛是中国的吗?','钓鱼岛是中国的',getdate()
    insert into Comments select 1,'绝对是',getdate()
    insert into Comments select 1,'必须是的',getdate()
    insert into Comments select 1,'谁说不是呢',getdate()
    

    评论内容的组合使用一个函数来实现,在函数中使用游标去遍历给定PostID的所有评论然后进行拼接,函数代码如下:

    create  FUNCTION fn_GetAllComments(@PostID int)
    RETURNS NVARCHAR(4000)
    AS
    BEGIN
        DECLARE @result VARCHAR(4000)
        SET @result=''
        DECLARE getAllComments CURSOR
        FOR
            select CommentID from Comments where PostID=@PostID
        OPEN getAllComments
        DECLARE @ID SYSNAME
        FETCH  FROM getAllComments INTO @ID
        WHILE @@fetch_status=0
        BEGIN
            SET @result=@result+(select convert(nvarchar(20),CreateDate,120) 
                                    from Comments where CommentID=@ID)+':'+
                                (select cast([Content] as nvarchar(4000)) 
                                    from Comments where CommentID=@ID)+';'
            FETCH  FROM getAllComments INTO @ID
        END
        CLOSE getAllComments
        SET @result= substring(@result,0,len(@result))
        DEALLOCATE getAllComments
        RETURN @result
    END

    现在写SQL语句来测试一下结果

    select 
        Title,[Content],CreateDate,dbo.fn_GetAllComments(PostID) as AllComments
    from Post

    查询结果如下:

    2010-10-15_153653

  • 相关阅读:
    maven POM.xml 标签详解
    Spring Boot Starter 的基本封装
    谷歌浏览器屏蔽广告的插件
    关于mysql中触发器old和new
    centos7 安装zookeeper3.4.8集群
    Flink架构、原理与部署测试
    图解Spark API
    汇编器构造
    Oracle11g CentOS7安装记录
    如何创造一门编程语言?
  • 原文地址:https://www.cnblogs.com/oec2003/p/1852282.html
Copyright © 2011-2022 走看看