SQL 如何在 Oracle 11g 中选择前五行或“N”行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20656521/
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 00:31:05  来源:igfitidea点击:

How to select top five or 'N' rows in Oracle 11g

sqloracleoracle11gsyntax-error

提问by Karthikeyan Sukkoor

select distinct ani_digit, ani_business_line from cta_tq_matrix_exp limit 5

I want to select top five rows from my resultset. if I used above query, getting syntax error. Thanks advance

我想从结果集中选择前五行。如果我使用上述查询,则会出现语法错误。提前致谢

回答by D Stanley

You'll need to use DISTINCTbeforeyou select the "top 5":

DISTINCT选择“前 5 个”之前,您需要使用:

SELECT * FROM 
(SELECT DISTINCT ani_digit, ani_business_line FROM cta_tq_matrix_exp) A
WHERE rownum <= 5

回答by Sai

  select distinct ani_digit, ani_business_line from cta_tq_matrix_exp where rownum<=5;

回答by Incognito

LIMITclause is not available in Oracle.

LIMIT子句在 Oracle 中不可用。

Seeing your query, you seem to be interested only in a certain number of rows (not ordered based on certain column value) and so you can use ROWNUMclause to limit the number of rows being returned.

看到您的查询,您似乎只对特定数量的行(不是根据特定列值排序)感兴趣,因此您可以使用ROWNUM子句来限制返回的行数。

select distinct ani_digit, ani_business_line from cta_tq_matrix_exp WHERE rownum <= 5

If you want to order the resultset and then limit the number of rows, you can modify your query as per the details in the link provided by Colin, in the comments above.

如果您想对结果集进行排序,然后限制行数,您可以根据 Colin 提供的链接中的详细信息修改您的查询,在上面的评论中。