在MySQL中,如果想实现将分组之后的多个数据合并到一列,可以使用group_concat函数,如下图所示:



但是,在Sybase中没有这样的函数(别问我为什么使用Sybase,因为公司用的Sybaseo(╯□╰)o)。因为我的Sybase是ASE的,使用变量累计的方法实现了该功能。憋说话,看代码:

IF OBJECT_ID('#test') IS NOT NULL
      drop table #test
go
CREATE TABLE #test(
id int null
,comment varchar(100) null
)
GO

insert into #test values(1,'111')
insert into #test values(1,'222')
insert into #test values(1,'333')
insert into #test values(1,'444')
insert into #test values(1,'555')
insert into #test values(1,'666')
insert into #test values(1,'777')
insert into #test values(2,'123')
insert into #test values(2,'456')
insert into #test values(2,'789')
insert into #test values(2,'012')
insert into #test values(2,'345')
insert into #test values(2,'678')
insert into #test values(3,'123')
insert into #test values(3,'456')
insert into #test values(3,'789')
insert into #test values(4,'123')
insert into #test values(4,'456')
insert into #test values(5,'234')
insert into #test values(6,'345')
insert into #test values(7,'789')
GO

BEGIN

    declare @cc varchar(500)
    declare @cc1 int
    declare @num int

    set @cc=''
    set @num=1
    select id,comment,space(500) as sub_comment,0000 as lev into #tt from #test order by id

    update #tt
    set sub_comment=(case when @cc1=id then @cc || ',' || comment else comment end)
        ,@cc=(case when @cc1=id then @cc || ',' || comment else comment end)
        ,lev=(case when @cc1=id then @num+1 else 1 end)
        ,@num=(case when @cc1=id then @num+1 else 1 end)
        ,@cc1=id

    select t.id,t.sub_comment
    from #tt t inner join (select id, max(lev) as tl from #tt group by id) c
        on t.id=c.id and t.lev=c.tl

--如果一个分组中的comment多于5个,最多显示5个comment
--     select t.id,t.sub_comment
--    from #tt t inner join (select id, (case when max(lev) > 5 then 5 else max(lev) end) as tl from #tt group by id) c
--        on t.id=c.id and t.lev=c.tl

    truncate table #tt
    drop table #tt

END

参考: http://bbs.csdn.net/topics/370026432

Sybase数据库实现等效的mysql中group_concat功能的更多相关文章

  1. oracle数据库不支持mysql中limit功能

    oracle数据库不支持mysql中limit功能,但可以通过rownum来限制返回的结果集的行数,rownum并不是用户添加的字段,而是oracle系统自动添加的. (1)使查询结果最多返回前10行 ...

  2. MySQL中group_concat函数-和group by配合使用

    MySQL中group_concat函数 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔 ...

  3. MySQL中group_concat函数深入理解

    本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . 一.MySQL中group_concat函数 完整的语法如下: gr ...

  4. MySQL中group_concat函数

    本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) .MySQL中group_concat函数完整的语法如下:group_c ...

  5. MySQL中group_concat函数 --- 很有用的一个用来查询出所有group by 分组后所有 同组内的 内容

    本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . MySQL中group_concat函数 完整的语法如下: grou ...

  6. mysql中group_concat函数用法

    该函数返回带有来自一个组的连接的非NULL值的字符串结果.该函数是一个增强的Sybase SQL Anywhere支持的基本LIST()函数. 语法结构: GROUP_CONCAT([DISTINCT ...

  7. MYSQL中group_concat有长度限制!默认1024

    在mysql中,有个函数叫"group_concat",平常使用可能发现不了问题,在处理大数据的时候,会发现内容被截取了,其实MYSQL内部对这个是有设置的,默认不设置的长度是10 ...

  8. MYSQL中group_concat有长度限制!默认1024(转载)

    在mysql中,有个函数叫“group_concat”,平常使用可能发现不了问题,在处理大数据的时候,会发现内容被截取了,其实MYSQL内部对这个是有设置的,默认不设置的长度是1024,如果我们需要更 ...

  9. MYSQL中group_concat( )函数中参数的排序方法

    使用mysql中的group_concat( )函数连接指定字段时,可以先对该字段进行排序. PS:是因为二刷mysql的51道题的第12题遇到的:查询和" 01 "号同学学习的课 ...

随机推荐

  1. jquery的ajax全局事件详解

        jquery在ajax方面是非常强大和方便的,以下是jquery进行ajax请求时方法模板: $.ajax({ type: "get", url: "" ...

  2. No mapping found for HTTP request with URI [/user/login.do] in DispatcherServlet with name 'dispatcher'错误

    1.警告的相关信息 七月 24, 2017 3:53:04 下午 org.springframework.web.servlet.DispatcherServlet noHandlerFound警告: ...

  3. 【Swift】swift中懒加载的写法

    swift中懒加载的写法,直接上例子 (懒加载一个遮罩视图) lazy var dummyView: UIView = { let v = UIView() v.backgroundColor = U ...

  4. java面试之String的理解(自我理解)

    1.String是基本数据类型吗? 不是,是对象,引用数据类型 2.String是可变吗? 不可变,String是final类型的. 3.怎样比较两个字符串的值相同,怎样比较两个字符串是否为同一对象? ...

  5. [HNOI 2004]树的计数

    Description 一个有n个结点的树,设它的结点分别为v1, v2, …, vn,已知第i个结点vi的度数为di,问满足这样的条件的不同的树有多少棵.给定n,d1, d2, …, dn,编程需要 ...

  6. bzoj 2560: 串珠子

    Description 铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个珠子和第j个珠子,可以选择不 ...

  7. POJ 2289(多重匹配+二分)

    POJ 2289(多重匹配+二分) 把n个人,分到m个组中.题目给出每一个人可以被分到的那些组.要求分配完毕后,最大的那一个组的人数最小. 用二分查找来枚举. #include<iostream ...

  8. Begin again

    新的一周开始. 写下这篇东西, 用来表明我还在奋斗的路上, 那么,加油咯.

  9. Linux添加系统调用的两种方法

    前言 系统调用的基本原理 系统调用其实就是函数调用,只不过调用的是内核态的函数,但是我们知道,用户态是不能随意调用内核态的函数的,所以采用软中断的方式从用户态陷入到内核态.在内核中通过软中断0X80, ...

  10. 5分钟快速打造WebRTC视频聊天

    百度一下WebRTC,我想也是一堆.本以为用这位朋友( 搭建WebRtc环境 )的SkyRTC-demo 就可以一马平川的实现聊天,结果折腾了半天,文本信息都发不出去,更别说视频了.于是自己动手. 想 ...