相当于DB2的LIMIT [英] Equivalent of LIMIT for DB2

查看:586
本文介绍了相当于DB2的LIMIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DB2 for iSeries中如何执行 LIMIT

How do you do LIMIT in DB2 for iSeries?

我有一个超过50,000条记录的表,我想将记录返回到10,000,记录10,000到20,000。

I have a table with more than 50,000 records and I want to return records 0 to 10,000, and records 10,000 to 20,000.

我在SQL中知道,在0到10,000的查询结尾处写入 LIMIT 0,10000 LIMIT 10000,10000 在10000到20,000的查询结尾处

I know in SQL you write LIMIT 0,10000 at the end of the query for 0 to 10,000 and LIMIT 10000,10000 at the end of the query for 10000 to 20,000

那么这样做在DB2中如何?什么代码和语法?
(完整的查询示例是赞赏)

So, how is this done in DB2? Whats the code and syntax? (full query example is appreciated)

推荐答案

开发此方法:

您需要具有可以排序的唯一值的表。

You NEED a table that has an unique value that can be ordered.

如果你想要行10,000到25,000行,你的表有40,000行,首先你需要得到起始点和总行:

If you want rows 10,000 to 25,000 and your Table has 40,000 rows, first you need to get the starting point and total rows:

int start = 40000 - 10000;

int total = 25000 - 10000;

然后通过这些查询的代码:

And then pass these by code to the query:

SELECT * FROM 
(SELECT * FROM schema.mytable 
ORDER BY userId DESC fetch first {start} rows only ) AS mini 
ORDER BY mini.userId ASC fetch first {total} rows only

这篇关于相当于DB2的LIMIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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