【问题标题】:How can I retrieve the Top N records from a query? [duplicate]如何从查询中检索前 N 条记录? [复制]
【发布时间】:2014-01-09 09:35:42
【问题描述】:

例如,如果我想从查询结果中检索前 3 条记录,该怎么办。我该怎么做?

【问题讨论】:

  • 在 MySQL 中使用 limitselect * from your_table order by some_column limit 3 但这仅在与排序顺序结合使用时才有意义。否则每次执行查询时结果都可能不同
  • 您的标签令人困惑。您是在 MySQL 还是 Oracle dbms 中寻找 SQL 语句? PL/SQL 是 Oracle dbms 中的一种编程语言,此处不需要。

标签: mysql oracle


【解决方案1】:
SELECT * FROM table1 where rownum <=3;

如果考虑订购则

select * from 
  (select * FROM table1 order by column_name)
 where rownum <=3;

但是等等,这个问题是如何同时与 Oracle 和 MySql 相关的?无论如何,这将在 Oracle 中工作。

【讨论】:

    【解决方案2】:

    你可以试试这个(适用于 db2 sql):

         select * from <table_name> fetch first 3 rows only with ur;
    

    问候

    【讨论】:

      猜你喜欢
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 2016-09-17
      相关资源
      最近更新 更多