如何将数据库表中前几条记录合并显示

本文介绍了一种在数据库中将特定数量的记录字段合并为一列的方法,通过SQL语句和Delphi程序实现,适用于需要汇总展示历史记录的场景。
如何将数据库表中前几条记录合并显示 Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiDB/html/delphi_20061222055338173.html
有一个数据库表有4个字段,第一个字段是ID,第二、三、四都是字符字段如:  
  ID     str1       str2     str3    
  1         Z             L             M  
  2         L             M             N  
  3         L             M             N  
  4         L             M             N  
            .....  
  我在程序中显示的时候,准备加一个字段,这样来进行显示  
  ID     str1         str2     str3       addstr  
  1         Z             L             M  
  2         L             M             N  
  3         L             M             N  
  4         L             M             N  
  5         N             N             L             ZLMLMNLMNLMNNNL  
  6         L             N             O             LMNLMNLMNNNLLNO  
        .....  
  也就是说,新添加的字段的内容就是前5条(包括自己本身的那条)记录1,2,3字段的合并显示,各位大侠想个招。。。。

10~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~分?!

不懂行情,一般这样的问题该多少分?

if   exists(select   1   from   sysobjects   where   id   =   object_id(N'testTable'))  
  drop   table   testTable    
   
  create   table   testTable    
  (id   int   primary   key   identity(1,1),  
  str1   varchar(1),  
  str2   varchar(1),  
  str3   varchar(1),  
  addstr   varchar(100)  
  )  
   
  insert   into   testTable   values('Z','L','M','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','T','')  
  go  
   
   
  alter   procedure   mytestProcedure  
  @FCurrentid   int  
  as  
   
  declare  
      @FieldStr   varchar(100),  
      @MaxId   int,  
      @CurrentID   int  
   
  set   @Fieldstr   =   ''  
  set   @CurrentID   =   @FCurrentid   +   1  
  select   @Maxid   =   max(id)   from   testtable  
     
  select   @FieldStr   =     @FieldStr   +   str1   +str2   +str3   from   testtable   where   id   <=   @FCurrentid  
   
  update   testtable   set   addstr   =   @FieldStr   where   id   =   @FCurrentid  
   
   
  while   @CurrentID   <   @MaxID   +1  
  begin  
      select   @FieldStr   =   right(@FieldStr,len(@FieldStr)-3*len(str1))   +   str1+str2+str3   from   testtable   where   id   =   @CurrentID  
      update   testtable   set   addstr   =   @FieldStr   where   id   =   @CurrentID  
     
      set   @CurrentID   =   @CurrentID   +   1  
  end  
  go  
   
  go  
   
  exec   mytestprocedure   5  
   
  select   *   from   testtable  
 

执行结果  
  ------------------  
  1 Z L M  
  2 L M N  
  3 L M N  
  4 L M N  
  5 L M N ZLMLMNLMNLMNLMN  
  6 L M N LMNLMNLMNLMNLMN  
  7 L M N LMNLMNLMNLMNLMN  
  8 L M N LMNLMNLMNLMNLMN  
  9 L M T LMNLMNLMNLMNLMT  
 

中间的while循环也可以使用游标实现,但效果都差不多,  
  应该还有更简单的方式可以一句话搞定,但我暂时没想到。

up

下面这种解决方式,代码可能更少些,也更容易懂些,但是,其内部执行的循环次数好像也更多些。  
  if   exists(select   1   from   sysobjects   where   id   =   object_id(N'testTable'))  
  drop   table   testTable    
   
  create   table   testTable    
  (id   int   primary   key   identity(1,1),  
  str1   varchar(1),  
  str2   varchar(1),  
  str3   varchar(1),  
  addstr   varchar(100)  
  )  
   
  insert   into   testTable   values('Z','L','M','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','N','')  
  insert   into   testTable   values('L','M','T','')  
  go  
   
  if   exists(select   1   from   sysobjects   where   id   =   object_id(N'myfunction'))  
  drop   function   myfunction    
  go  
   
  --自定义函数,根据传递的创始化id(就是从第几行开始),和当前id来返回一个当前id前面几行的一个字符串;  
  create   function   myfunction   (@FinitID   int,@FCurrID   int)  
  returns   varchar(100)  
  as  
  begin  
  declare  
      @FieldStr   varchar(100)  
   
  set   @Fieldstr   =   ''  
     
  select   @FieldStr   =     @FieldStr   +   str1   +str2   +str3    
  from   testtable    
  where   id   between   (@FCurrID   -   @FinitID   +1)   and   @FCurrID  
   
  return(@FieldStr)    
   
  end  
   
  go  
  update   testtable   set   addstr   =   test.dbo.myfunction(5,id)   from   testtable   where   id   >4  
  select   *   from   testtable

CREATE   FUNCTION     AddColumn   (@ID   INT)      
  RETURNS   NVARCHAR(4000)   AS      
  BEGIN    
  Declare   @SQL   NVARCHAR(4000)  
  Declare   @ROW   INT                
  Select   @SQL   =   ''  
  Select   @ROW   =   COUNT(1)     FROM   (Select   Top   5   *   from   TADD   Where   ID   <=   @ID   Order   By   ID   Desc)   T  
  IF   @ROW   =   5    
  Begin  
  Select   @SQL   =   @SQL   +   Str   From   (Select   Top   5   Str1+Str2+Str3   Str   from   (Select   Top   5   *   from   TADD   Where   ID   <=   @ID   Order   By   ID   Desc   )   T     Order   By   ID   )   TT  
  End  
  Return   @SQL    
  END  
   
  -----------------------------  
   
  Select   ID,   STR1,   STR2,   STR3,   DBO.AddAtr(ID)   ADDSTR   FROM   TADD

Select   ID,   STR1,   STR2,   STR3,   DBO.AddColumn(ID)   ADDSTR   FROM   TADD  
 

谢谢几位的解答,不过,这和我的目的还是有些差距,可能我没有说清楚,我是想数据库不动,在DELPHI的ADOTABLE中添加一个字段,在OnCalcFields事件中用什么方法能够将上面的目的达到!

 
      重写VCL

经过不断的实验,自己解决了,就是比较麻烦,不过还是谢谢大家了,给两位回答问题积极的帅哥送点分!!

将解决的办法说一下,TABLE表不动,添加一个计算字段,然后,做一个自定义函数,将ID传给函数,在函数中包含一个动态查询,做一个循环的查询将结果累计,然后返回给TABLE的oncalcfields过程中的计算字段的赋值。

转载于:https://www.cnblogs.com/delphi2007/archive/2008/12/11/1352506.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值