<small id='Pt4lM'></small><noframes id='Pt4lM'>

        <bdo id='Pt4lM'></bdo><ul id='Pt4lM'></ul>

        <tfoot id='Pt4lM'></tfoot>
        <i id='Pt4lM'><tr id='Pt4lM'><dt id='Pt4lM'><q id='Pt4lM'><span id='Pt4lM'><b id='Pt4lM'><form id='Pt4lM'><ins id='Pt4lM'></ins><ul id='Pt4lM'></ul><sub id='Pt4lM'></sub></form><legend id='Pt4lM'></legend><bdo id='Pt4lM'><pre id='Pt4lM'><center id='Pt4lM'></center></pre></bdo></b><th id='Pt4lM'></th></span></q></dt></tr></i><div id='Pt4lM'><tfoot id='Pt4lM'></tfoot><dl id='Pt4lM'><fieldset id='Pt4lM'></fieldset></dl></div>

        <legend id='Pt4lM'><style id='Pt4lM'><dir id='Pt4lM'><q id='Pt4lM'></q></dir></style></legend>

        使用 rownum 选择表格的第二行

        时间:2023-09-18

            <i id='omFeR'><tr id='omFeR'><dt id='omFeR'><q id='omFeR'><span id='omFeR'><b id='omFeR'><form id='omFeR'><ins id='omFeR'></ins><ul id='omFeR'></ul><sub id='omFeR'></sub></form><legend id='omFeR'></legend><bdo id='omFeR'><pre id='omFeR'><center id='omFeR'></center></pre></bdo></b><th id='omFeR'></th></span></q></dt></tr></i><div id='omFeR'><tfoot id='omFeR'></tfoot><dl id='omFeR'><fieldset id='omFeR'></fieldset></dl></div>

              <small id='omFeR'></small><noframes id='omFeR'>

                  <bdo id='omFeR'></bdo><ul id='omFeR'></ul>
                  <tfoot id='omFeR'></tfoot>
                    <tbody id='omFeR'></tbody>
                  <legend id='omFeR'><style id='omFeR'><dir id='omFeR'><q id='omFeR'></q></dir></style></legend>

                  本文介绍了使用 rownum 选择表格的第二行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试了以下查询:

                  select empno from (
                                     select empno 
                                       from emp
                                      order by sal desc
                                    )
                  where rownum = 2
                  

                  这不会返回任何记录.

                  当我尝试这个查询时

                   select rownum,empno from (
                                          select empno from emp order by sal desc) 
                  

                  它给了我这个输出:

                  ROWNUM  EMPNO      
                  1       7802        
                  2       7809    
                  3       7813    
                  4       7823
                  

                  谁能告诉我我的第一个查询有什么问题?为什么添加ROWNUM过滤器时不返回任何记录?

                  Can anyone tell me what's the problem with my first query? Why is it not returning any records when I add the ROWNUM filter?

                  推荐答案

                  为了解释这种行为,我们需要了解 Oracle 如何处理行号.给一行赋值 ROWNUM 时,Oracle 从 1 开始,仅在选择一行时增加值;也就是说,当所有WHERE 子句中的条件得到满足.由于我们的条件需要ROWNUM 大于 2,未选择任何行且 ROWNUM 为永远不会超过 1.

                  To explain this behaviour, we need to understand how Oracle processes ROWNUM. When assigning ROWNUM to a row, Oracle starts at 1 and only increments the value when a row is selected; that is, when all conditions in the WHERE clause are met. Since our condition requires that ROWNUM is greater than 2, no rows are selected and ROWNUM is never incremented beyond 1.

                  最重要的是,以下条件将作为预期.

                  The bottom line is that conditions such as the following will work as expected.

                  ... WHERE rownum = 1;

                  .. WHERE rownum = 1;

                  ... WHERE rownum <= 10;

                  .. WHERE rownum <= 10;

                  虽然具有这些条件的查询将始终返回零行.

                  While queries with these conditions will always return zero rows.

                  ...WHERE rownum = 2;

                  .. WHERE rownum = 2;

                  ... WHERE rownum > 10;

                  .. WHERE rownum > 10;

                  引自了解 Oracle rownum

                  您应该以这种方式修改您的查询以便工作:

                  You should modify you query in this way in order to work:

                  select empno
                  from
                      (
                      select empno, rownum as rn 
                      from (
                            select empno
                            from emp
                            order by sal desc
                            )
                      )
                  where rn=2;
                  

                  EDIT:我已经更正了查询以获取 rownum after 由 sal desc 排序

                  EDIT: I've corrected the query to get the rownum after the order by sal desc

                  这篇关于使用 rownum 选择表格的第二行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 sqlplus/Oracle 中将纪元转换为日期 下一篇:如何从oracle的表中选择偶数记录?

                  相关文章

                  <small id='rrpqO'></small><noframes id='rrpqO'>

                    <bdo id='rrpqO'></bdo><ul id='rrpqO'></ul>
                • <i id='rrpqO'><tr id='rrpqO'><dt id='rrpqO'><q id='rrpqO'><span id='rrpqO'><b id='rrpqO'><form id='rrpqO'><ins id='rrpqO'></ins><ul id='rrpqO'></ul><sub id='rrpqO'></sub></form><legend id='rrpqO'></legend><bdo id='rrpqO'><pre id='rrpqO'><center id='rrpqO'></center></pre></bdo></b><th id='rrpqO'></th></span></q></dt></tr></i><div id='rrpqO'><tfoot id='rrpqO'></tfoot><dl id='rrpqO'><fieldset id='rrpqO'></fieldset></dl></div>

                    <tfoot id='rrpqO'></tfoot>

                    1. <legend id='rrpqO'><style id='rrpqO'><dir id='rrpqO'><q id='rrpqO'></q></dir></style></legend>