Oracle 10g,选择子句

发布于 2024-10-18 02:52:59 字数 51 浏览 3 评论 0原文

假设一个有 14 行的员工表,我只想选择中间行,例如 3 到 10 行,那么如何做到呢?

suppose a table employees with 14 rows and i want to select only middle rows like from 3 to 10 then how it can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白云不回头 2024-10-25 02:52:59

首先,您必须定义一个顺序。如果没有顺序,“中间行,如 3 到 10”是没有意义的。当您知道顺序时,您可以使用与 RC 提议的几乎相同的查询:

select *
  from ( select e.*
              , rownum rn
           from emp e
          order by <your ordering columns here>
       )
 where rn between 3 and 10

问候,
抢。

First you'll have to define an ordering. Without an ordering, "middle rows like from 3 to 10" is meaningless. When you know the ordering, you can use almost the same query as RC proposed:

select *
  from ( select e.*
              , rownum rn
           from emp e
          order by <your ordering columns here>
       )
 where rn between 3 and 10

Regards,
Rob.

再可℃爱ぅ一点好了 2024-10-25 02:52:59
SELECT * FROM (SELECT e.*, rownum r FROM emp e) WHERE r BETWEEN 3 AND 10;
SELECT * FROM (SELECT e.*, rownum r FROM emp e) WHERE r BETWEEN 3 AND 10;
总攻大人 2024-10-25 02:52:59

如果您正在查找行的随机抽样,则可以使用样本子句。然而,它是基于百分比的。

从表样本 (5) 中选择 *;
-- 5%

If you are looking for a random sampling of rows, you can use the sample clause. It is based on percentages, however.

select * from table sample (5);
-- 5 percent

合久必婚 2024-10-25 02:52:59

如果您的要求是获取“除了前 3 行和后 3 行之外的所有行”,无论表中有多少行,您可以使用以下方法:

select * from (
  select emp.*,
         row_number() over (order by id) n,
         count(*) over () c
  from emp)
where n between 4 and c - 4;

如果您的要求是获取“除了前 20 行之外的所有行” % 和底部 20%”,你可以使用这个:

select * from (
  select emp.*,
         ntile(5) over (order by id) n,
         count(*) over () c
  from emp)
where n between 2 and 4;

If your requirement is to get "all rows except for the top 3 and the bottom 3", regardless of the number of rows in the table, you could use this:

select * from (
  select emp.*,
         row_number() over (order by id) n,
         count(*) over () c
  from emp)
where n between 4 and c - 4;

If your requirement is to get "all the rows except for the top 20% and the bottom 20%", you could use this:

select * from (
  select emp.*,
         ntile(5) over (order by id) n,
         count(*) over () c
  from emp)
where n between 2 and 4;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文