如何从oracle sql中只选择1行?

发布于 2024-12-28 03:42:42 字数 289 浏览 1 评论 0原文

我想使用 oracle 语法从表 DUAL 中仅选择 1 行。例如,我想执行这个查询:

SELECT user 
  FROM DUAL

...它有 40 条记录。但我只需要一张记录。 ...而且,我想让它在没有 WHERE 子句的情况下发生。

我需要在 table_name 字段中添加一些内容,例如:

SELECT FirstRow(user) 
  FROM DUAL

I want to use oracle syntax to select only 1 row from table DUAL. For example, I want to execute this query:

SELECT user 
  FROM DUAL

...and it'd have, like, 40 records. But I need only one record. ...AND, I want to make it happen without a WHERE clause.

I need something in the table_name field such as:

SELECT FirstRow(user) 
  FROM DUAL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

听不够的曲调 2025-01-04 03:42:42

您使用 ROWNUM。

IE。

SELECT user FROM Dual WHERE ROWNUM = 1

http://docs.oracle.com/cd/B19306_01/server.102/b14200/伪列009.htm

You use ROWNUM.

ie.

SELECT user FROM Dual WHERE ROWNUM = 1

http://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns009.htm

妄司 2025-01-04 03:42:42

Oracle 12c 中提供了此语法:

select * from some_table fetch first 1 row only;
select * from some_table fetch first 1 rows only;
select * from some_table fetch first 10 row only;
select * from some_table fetch first 10 rows only;

^^我只是想演示可以使用 row 或 rows(复数),而不管所需行数是多个。)

This syntax is available in Oracle 12c:

select * from some_table fetch first 1 row only;
select * from some_table fetch first 1 rows only;
select * from some_table fetch first 10 row only;
select * from some_table fetch first 10 rows only;

^^I just wanted to demonstrate that either row or rows (plural) can be used regardless of the plurality of the desired number of rows.)

猫瑾少女 2025-01-04 03:42:42

我发现这个“解决方案”隐藏在其中一条评论中。因为我已经查找了一段时间,所以我想稍微强调一下它(还不能评论或做这样的事情......),所以这就是我使用的:

SELECT * FROM (SELECT [Column] FROM [Table] ORDER BY [Date] DESC) WHERE ROWNUM = 1

这将打印我想要的[列]表中最新条目的条目,假设 [Date] 始终通过 SYSDATE 插入。

I found this "solution" hidden in one of the comments. Since I was looking it up for a while, I'd like to highlight it a bit (can't yet comment or do such stuff...), so this is what I used:

SELECT * FROM (SELECT [Column] FROM [Table] ORDER BY [Date] DESC) WHERE ROWNUM = 1

This will print me the desired [Column] entry from the newest entry in the table, assuming that [Date] is always inserted via SYSDATE.

烟织青萝梦 2025-01-04 03:42:42

我们有 3 个选择来获取 Oracle DB 表中的第一行。

1) select * from table_name where rownum= 1 是最好的方法

2) select * from table_name where id = ( select min(id) from table_name)

3)

select * from 
    (select * from table_name order by id)
where rownum = 1

we have 3 choices to get the first row in Oracle DB table.

1) select * from table_name where rownum= 1 is the best way

2) select * from table_name where id = ( select min(id) from table_name)

3)

select * from 
    (select * from table_name order by id)
where rownum = 1
怼怹恏 2025-01-04 03:42:42

您应该使用嵌套查询:

SELECT *
FROM ANY_TABLE_X 
WHERE ANY_COLUMN_X = (SELECT MAX(ANY_COLUMN_X) FROM ANY_TABLE_X) 

=>在 PL/SQL 中,“ROWNUM = 1”不等于 TSQL 的“TOP 1”。

因此,您不能使用这样的查询:“select * from any_table_x where rownum=1 order by any_column_x;”因为 Oracle 获取第一行然后应用 order by 子句。

You should use nested query as:

SELECT *
FROM ANY_TABLE_X 
WHERE ANY_COLUMN_X = (SELECT MAX(ANY_COLUMN_X) FROM ANY_TABLE_X) 

=> In PL/SQL "ROWNUM = 1" is NOT equal to "TOP 1" of TSQL.

So you can't use a query like this: "select * from any_table_x where rownum=1 order by any_column_x;" Because oracle gets first row then applies order by clause.

扎心 2025-01-04 03:42:42

据我所知,Oracle中的dual表是一种只有一行的特殊表。所以,这就足够了:

SELECT user
FROM dual

As far as I know, the dual table in Oracle is a special table with just one row. So, this would suffice:

SELECT user
FROM dual
软糖 2025-01-04 03:42:42

Oracle中没有limit 1条件(即MySQL / PostgresSQL),您需要指定where rownum = 1

There is no limit 1 condition (thats MySQL / PostgresSQL) in Oracle, you need to specify where rownum = 1.

陌上芳菲 2025-01-04 03:42:42

“FirstRow”是一个限制,因此它位于 where 子句中,而不是位于 select 子句中。它叫做 rownum

select * from dual where rownum = 1;

"FirstRow" Is a restriction and therefor it's place in the where clause not in the select clause. And it's called rownum

select * from dual where rownum = 1;
临风闻羌笛 2025-01-04 03:42:42

如果您只想返回子查询最少的排序结果的第一行,请尝试以下操作:

select *
  from ( select a.*
              , row_number() over ( order by sysdate_col desc ) as row_num
           from table_name a )
  where row_num = 1;

If you want to get back only the first row of a sorted result with the least subqueries, try this:

select *
  from ( select a.*
              , row_number() over ( order by sysdate_col desc ) as row_num
           from table_name a )
  where row_num = 1;
祁梦 2025-01-04 03:42:42

如果任何行都可以,请尝试:

select max(user)  
from table;

无 where 子句。

If any row would do, try:

select max(user)  
from table;

No where clause.

初见你 2025-01-04 03:42:42
select name, price
  from (
    select name, price, 
    row_number() over (order by price) r
      from items
  )
where r between 1 and 5; 
select name, price
  from (
    select name, price, 
    row_number() over (order by price) r
      from items
  )
where r between 1 and 5; 
抹茶夏天i‖ 2025-01-04 03:42:42

select a.user from (select user from users order by user) a where rownum = 1

将执行最佳操作,另一个选项是:

select a.user 
from ( 
select user, 
row_number() over (order by user) user_rank, 
row_number() over (partition by dept order by user) user_dept_rank 
from users 
) a 
where a.user_rank = 1 or user_dept_rank = 2

在您想要不同子集的情况下,但我想您也可以使用RANK() 但是,我也喜欢 row_number() over(...) 因为不需要分组。

select a.user from (select user from users order by user) a where rownum = 1

will perform the best, another option is:

select a.user 
from ( 
select user, 
row_number() over (order by user) user_rank, 
row_number() over (partition by dept order by user) user_dept_rank 
from users 
) a 
where a.user_rank = 1 or user_dept_rank = 2

in scenarios where you want different subsets, but I guess you could also use RANK() But, I also like row_number() over(...) since no grouping is required.

勿忘初心 2025-01-04 03:42:42

select max() 更灵活的是:

select distinct first_row(column_x) over (order by column_y,column_z,...) from Table_A

More flexible than select max() is:

select distinct first_row(column_x) over (order by column_y,column_z,...) from Table_A
逐鹿 2025-01-04 03:42:42

在 oracle 11g 中,下面的例子可能会有所帮助......

 SELECT *
 FROM
 (
    SELECT *
        
    FROM RF_ACTN_STATUS STS 
    WHERE 
        STS.RF_ACTN_TYPE_ID=27 AND 
        STS.ACTION_SORT>2 AND 
        NVL(STS.IS_RESEND,'N')='N' AND 
        NVL(STS.IS_CANCELLED_STATIUS,'N')='N'            
    ORDER BY STS.ACTION_SORT ASC
) X
WHERE ROWNUM = 1
        ;

In oracle 11g, The below example can be helpful....

 SELECT *
 FROM
 (
    SELECT *
        
    FROM RF_ACTN_STATUS STS 
    WHERE 
        STS.RF_ACTN_TYPE_ID=27 AND 
        STS.ACTION_SORT>2 AND 
        NVL(STS.IS_RESEND,'N')='N' AND 
        NVL(STS.IS_CANCELLED_STATIUS,'N')='N'            
    ORDER BY STS.ACTION_SORT ASC
) X
WHERE ROWNUM = 1
        ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文