在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. SpringBoot(五):@ConfigurationProperties配置参数绑定

    在springmvc或其他ssh框架中如果我们要实现一个配置参数的加载,需要使用代码实现读取properties文件等操作,或者需要使用其他属性@value(name="username&q ...

  2. 个人网站建设(适合Java初学者)(一)

    概述 作为一个在八本学校在校生,没有实验室,也没有项目可做.一直想做一个个人博客,一年前学完javaweb之后做了一个简单的博客,ui惨不忍睹就算了,还有各种bug.酝酿了很久,寒假用了将近一个月时间 ...

  3. with工作原理

    进入时,调用对象的__enter__ 退出时,调用对象的__exit__

  4. win10安装Ubuntu14.04双系统

    1 制作镜像 UltralISO刻录镜像到U盘,下载地址:http://pan.baidu.com/s/1o7JpthS 2压缩空间给Ubuntu安装 使用windows自带的压缩(磁盘管理) 3安装 ...

  5. SpringBoot开发案例之多任务并行+线程池处理

    前言 前几篇文章着重介绍了后端服务数据库和多线程并行处理优化,并示例了改造前后的伪代码逻辑.当然了,优化是无止境的,前人栽树后人乘凉.作为我们开发者来说,既然站在了巨人的肩膀上,就要写出更加优化的程序 ...

  6. spring copy中的一个很气人的问题(初学者渣渣的一些感受)

    把别人的工程直接导入使用,出现了各种bug......(细节决定成败,得到以下教训) 1.工程的第一步是检查版本和插件版本兼容问题.很重要 2.然后导入包,看依赖包是否版本太低,(前期做好这些,能让你 ...

  7. pyqt4 写动画不能播放问题集合

    最近在学习动画,真的真的是血泪史,百度基本是0资源,各种在谷歌外国大佬的英文中躺过一个一个血坑....... 这是随便写的一个动画功能调试窗口..... 问题现象: 点击食灵,没反应,写的动画不能生成 ...

  8. ●BZOJ 2694 Lcm

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2694 题解: 莫比乌斯反演 不难看出,造成贡献的(i,j)满足gcd(i,j)无平方因子. ...

  9. 【HDU 2966 k-dimensional Tree 入个门】

    ·“k-d树是一种分割k维数据空间的数据结构.主要应用于多维空间关键数据的范围搜索和最近邻搜索……”’'   ·英文题,述大意:      给出平面内n个点(n<=100000,0<=x, ...

  10. hdu 1890 splay树

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...