如何从oracle sql中只选择1行?
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您使用 ROWNUM。
IE。
http://docs.oracle.com/cd/B19306_01/server.102/b14200/伪列009.htm
You use ROWNUM.
ie.
http://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns009.htm
Oracle 12c 中提供了此语法:
^^我只是想演示可以使用 row 或 rows(复数),而不管所需行数是多个。)
This syntax is available in Oracle 12c:
^^I just wanted to demonstrate that either row or rows (plural) can be used regardless of the plurality of the desired number of rows.)
我发现这个“解决方案”隐藏在其中一条评论中。因为我已经查找了一段时间,所以我想稍微强调一下它(还不能评论或做这样的事情......),所以这就是我使用的:
这将打印我想要的[列]表中最新条目的条目,假设 [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:
This will print me the desired [Column] entry from the newest entry in the table, assuming that [Date] is always inserted via SYSDATE.
我们有 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)
we have 3 choices to get the first row in Oracle DB table.
1)
select * from table_name where rownum= 1
is the best way2)
select * from table_name where id = ( select min(id) from table_name)
3)
您应该使用嵌套查询:
=>在 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:
=> 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.
据我所知,Oracle中的
dual
表是一种只有一行的特殊表。所以,这就足够了:As far as I know, the
dual
table in Oracle is a special table with just one row. So, this would suffice:Oracle中没有
limit 1
条件(即MySQL / PostgresSQL),您需要指定where rownum = 1
。There is no
limit 1
condition (thats MySQL / PostgresSQL) in Oracle, you need to specifywhere rownum = 1
.“FirstRow”是一个限制,因此它位于
where
子句中,而不是位于select
子句中。它叫做 rownum"FirstRow" Is a restriction and therefor it's place in the
where
clause not in theselect
clause. And it's called rownum如果您只想返回子查询最少的排序结果的第一行,请尝试以下操作:
If you want to get back only the first row of a sorted result with the least subqueries, try this:
如果任何行都可以,请尝试:
无 where 子句。
If any row would do, try:
No where clause.
select a.user from (select user from users order by user) a where rownum = 1
将执行最佳操作,另一个选项是:
在您想要不同子集的情况下,但我想您也可以使用
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:
in scenarios where you want different subsets, but I guess you could also use
RANK()
But, I also likerow_number()
over(...)
since no grouping is required.比
select max()
更灵活的是:More flexible than
select max()
is:在 oracle 11g 中,下面的例子可能会有所帮助......
In oracle 11g, The below example can be helpful....