仅显示 SQL 查询输出的前 N ​​行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/874082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:07:26  来源:igfitidea点击:

Show only the first N lines of output of a SQL query

sql

提问by pacman

Is there a way to only show the first N lines of output from an SQLquery?
Bonus points, if the query stops running once the Nlines are outputted.

有没有办法只显示SQL查询输出的前 N ​​行?
奖励积分,如果查询N在输出行后停止运行。

I am most interested in finding something which works in Oracle.

我最感兴趣的是找到在Oracle.

回答by Rutesh Makhijani

It would be helpful if you specify what database you are targetting. Different databases have different syntax and techniques to achieve this:

如果您指定要定位的数据库,这将很有帮助。不同的数据库有不同的语法和技术来实现这一点:

For example in Oracle you can ahieve this by putting condition on RowNum(select ... from ... where ... rownum < 11-> would result in outputting first 10 records)

例如,在 Oracle 中,您可以通过将条件置于RowNum( select ... from ... where ... rownum < 11-> 将导致输出前 10 条记录) 来实现这一点

In MySQLyou can use you can use limitclause.

In MySQLyou can use you can uselimit子句。

Microsoft SQL Server =>SELECT TOP 10 column FROM table

微软 SQL Server =>SELECT TOP 10 column FROM table

PostgreSQL and MySQL =>SELECT column FROM table LIMIT 10

PostgreSQL 和 MySQL =>SELECT column FROM table LIMIT 10

Oracle =>select * from (SELECT column FROM table ) WHERE ROWNUM <= 10(thanks to stili)

Oracle =>select * from (SELECT column FROM table ) WHERE ROWNUM <= 10(感谢 stili)

Sybase =>SET rowcount 10 SELECT column FROM table

Sybase =>SET rowcount 10 SELECT column FROM table

Firebird =>SELECT FIRST 10 column FROM table

火鸟=>SELECT FIRST 10 column FROM table

NOTE: Modern ORMtools such as Hibernate give high level API (Query, Restriction, Condition interfaces) that abstract the logic of top n rows based on the dialect you choose.

注意:ORMHibernate 等现代工具提供高级 API(查询、限制、条件接口),可根据您选择的方言抽象前 n 行的逻辑。

回答by stili

For Oracle the suggested and accepted solution is wrong. Try using an order clause, and the results will be unpredictable. The SQL will need to be nested to accomplish this in Oracle.

对于 Oracle,建议和接受的解决方案是错误的。尝试使用 order 子句,结果将是不可预测的。需要嵌套 SQL 才能在 Oracle 中完成此操作。

select name, price
  from (
    select name, price, row_number() over (order by price) r
      from items
  )
where r between 1 and 5; 

The example above was borrowed from http://www.adp-gmbh.ch/ora/sql/examples/first_rows.htmlwhich has a good discussion on this topic.

上面的例子是从http://www.adp-gmbh.ch/ora/sql/examples/first_rows.html借来的,它对这个话题有很好的讨论。

回答by Olivier Pons

I know it with MySQL but I don't know if it's standard SQL : end you Query with 'limit X', X = n. of lines you want to get.

我知道 MySQL,但我不知道它是否是标准 SQL:以 'limit X', X = n 结束您的查询。您想要获得的行数。

Example :

例子 :

SELECT NAME FROM EMPLOYEES ORDER BY SALARY DESC LIMIT 10;

SELECT NAME FROM EMPLOYEES ORDER BY SALARY DESC LIMIT 10;

回答by CSK

For Oracle, you can try this

对于 Oracle,你可以试试这个

select /*+ FIRST_ROWS(10) */ * from table;