How do I do top 1 in Oracle?

2019-01-01 10:50发布

How do I do the following?

select top 1 Fname from MyTbl

In Oracle 11g?

9条回答
低头抚发
2楼-- · 2019-01-01 10:55

With Oracle 12c (June 2013), you are able to use it like the following.

SELECT * FROM   MYTABLE
--ORDER BY COLUMNNAME -OPTIONAL          
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
查看更多
情到深处是孤独
3楼-- · 2019-01-01 10:55

Use:

SELECT x.*
  FROM (SELECT fname 
          FROM MyTbl) x
 WHERE ROWNUM = 1

If using Oracle9i+, you could look at using analytic functions like ROW_NUMBER() but they won't perform as well as ROWNUM.

查看更多
一个人的天荒地老
4楼-- · 2019-01-01 10:58
select * from (
    select FName from MyTbl
)
where rownum <= 1;
查看更多
余生请多指教
5楼-- · 2019-01-01 11:04

If you want just a first selected row, you can:

select fname from MyTbl where rownum = 1

You can also use analytic functions to order and take the top x:

select max(fname) over (rank() order by some_factor) from MyTbl
查看更多
人气声优
6楼-- · 2019-01-01 11:09
SELECT *
  FROM (SELECT * FROM MyTbl ORDER BY Fname )
 WHERE ROWNUM = 1;
查看更多
时光乱了年华
7楼-- · 2019-01-01 11:11

You can do something like

    SELECT *
      FROM (SELECT Fname FROM MyTbl ORDER BY Fname )
 WHERE rownum = 1;

You could also use the analytic functions RANK and/or DENSE_RANK, but ROWNUM is probably the easiest.

查看更多
登录 后发表回答