如何重写表中的最后5条记录? [英] How to reterive the last 5 records in a table?

查看:53
本文介绍了如何重写表中的最后5条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个包含行数的表。我想从表中获取最后5条记录的

。我知道对于第一个n记录我们可以使用FETCH FIRST n ROWS ONLY命令来使用
。但我想获取最后5

的记录。任何人都可以帮忙...


预付谢谢你的回复

温馨的问候

大师

解决方案

Guru写道:



我有一张表包含行数。我想从表中获取最后5条记录。我知道对于第一个n记录,我们可以使用FETCH FIRST n ROWS ONLY命令。但是我想获取最后5条记录。任何人都可以帮忙...


预付谢谢你的回复

温馨的问候
大师



FETCH FIRST 5 ROWS不从表中获取前五个记录

除非你已经在表中添加了一个聚集索引,并且你已经包含了一个订单您的select语句的子句导致

优化器在其计划中选择聚簇索引。 (从技术上讲,只有在创建聚簇索引(或执行表重组)和
时,才能保证
为真。 />
在执行下一个表插入之前。)FETCH FIRST 5 ROWS选择

从返回的RESULT集开始的前5行,进一步,相同的5
$ b如果重复运行select语句

,则不一定返回$ b行。 (对于

a并行数据库运行时,这最后一个声明特别明显。)这是因为数据库

返回由select语句选择的合格行的顺序是

非确定性(即随机)。


鉴于上述情况,因此可以理解的是,没有

FETCH LAST n ROWS命令可用。如果你仍然只想检查你的select返回的最后五行

,最简单的方法是执行此操作

(在shell脚本中或从命令行)如下:


db2 -x" select blah-blah-blah" |尾巴-5


如上所述,即使您选择的表格没有变化,每次运行
时也会产生不同的结果。


" Guru" <顾********* @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...



我有一个包含行数的表。我想从表中获取最后5条记录。我知道对于第一个n记录,我们可以使用FETCH FIRST n ROWS ONLY命令。但是我想获取最后5条记录。任何人都可以帮忙......
温馨的问候
大师




如何按降序排序结果集然后使用FETCH

仅限第一行。




但实际问题是大师的意思是什么是顺序

行重试,他想获得最后5行..


Thiru。

WantedToBeDBA。


Hi

I have a table which contains number of rows. I want to fetch the
last 5 records from the table. I know for the first ''n'' records we can
use FETCH FIRST n ROWS ONLY command. But i want to fetch last 5
records. Can any one help in this...

Advance Thanks for ur replies
Warm Regards
Guru

解决方案

Guru wrote:

Hi

I have a table which contains number of rows. I want to fetch the
last 5 records from the table. I know for the first ''n'' records we can
use FETCH FIRST n ROWS ONLY command. But i want to fetch last 5
records. Can any one help in this...

Advance Thanks for ur replies
Warm Regards
Guru


FETCH FIRST 5 ROWS does NOT fetch the first five records from the table
UNLESS you have added a clustered index to the table and you have
included an order by clause to your select statement that causes the
optimizer to select the clustered index in its plan. (Technically, this
is only guaranteed to be true if the select statement is run immediately
after the clustered index is created (or a table reorg is performed) and
before the next table insert is performed.) FETCH FIRST 5 ROWS selects
the first 5 rows from the returned RESULT set and, further, the same 5
rows are not necessarily returned if the select statement is run
repeatedly. (This last statement is especially evident when run against
a parallel database.) This is because the order in which the database
returns qualifying rows selected by a select statement is
non-deterministic (i.e. random).

Given the above, it is therefore understandable that there is not a
FETCH LAST n ROWS command available. If you still desire to examine only
the last five rows returned by your select, the easiest way to do this
(in a shell script or from the command line) would be as follows:

db2 -x "select blah-blah-blah" | tail -5

As explained above, this can yield different results every time it is
run, even if the table you are selecting from does not change.


"Guru" <gu*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

Hi

I have a table which contains number of rows. I want to fetch the
last 5 records from the table. I know for the first ''n'' records we can
use FETCH FIRST n ROWS ONLY command. But i want to fetch last 5
records. Can any one help in this...
Warm Regards
Guru



How about sorting the result set in descending order and then use FETCH
FIRST n ROWS ONLY.


Hi,
But the actually question what guru mean is whatever be the order of
rows retried, he wanted to get last 5 rows..

Thiru.
WantedToBeDBA.


这篇关于如何重写表中的最后5条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆