如何将数据库表中前几条记录合并显示
2008-12-11 10:00
169 查看
如何将数据库表中前几条记录合并显示 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过程中的计算字段的赋值。
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过程中的计算字段的赋值。
相关文章推荐
- 如何让从数据库中查询出符合条件的记录,然后给每个记录自动加上序号,序号是根据符合条件的记录多少在随时变化.而且也会在以查询生成的报表中显示出来.谢谢
- top 10问题,如何在不同的数据库中查询前几条记录?
- 转 查询前几条记录SQL在不同数据库中的用法
- 【C#】如何在查询数据库表并显示到DataGridV中的进度条动态显示?
- 如何:在 Calendar 控件中显示数据库中的选定日期
- 如何在Spread ASP.NET中显示数据库的数据关系?
- oracle中如何用sql语句显示当前的数据库名称?
- 请教,如何将image显示的图相保存到数据库中?
- SQLServer中如何将一个字段的多个记录值合在一行显示
- 如何去掉数据库重复记录并且只保留一条记录
- 如何显示数据库long text 字段内容
- 3.如何优化操作大数据量数据库(实现小数据量和海量数据的分页显示存储过程)
- 如何瞬间在数据库插入一百万条记录
- 好几条记录只是仓位及数量不同,其它内容都一样,现在要显示出一条来,仓位及数量用"/"分开
- 如何在SQLServer中处理每天四亿三千万记录的(数据库大数据处理)
- Linux 下如何 sql*plus 实现上下键历史记录显示(安装rlwrap 以及 readline)
- 如何写一个数据库触发器,在插入记录时触发
- 在不同数据库中查询前几条记录的方法
- 刚刚做了一个小项目,存在一个问题,就是如何在数据库中插入多条记录