Oracle SQL Select 中的行数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2884183/
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
Number of rows in Oracle SQL Select?
提问by twelshesgi
I need to know how many records were returned in a select in oracle. Currently, I do two queries:
我需要知道在 oracle 的选择中返回了多少条记录。目前,我做了两个查询:
SELECT COUNT(ITEM_ID) FROM MY_ITEMS;
SELECT * FROM MY_ITEMS;
I need to know the COUNT but I hate doing two queries. Is there a way to do:
我需要知道 COUNT 但我讨厌做两个查询。有没有办法做到:
SELECT * FROM MY_ITEMS
and then find out how many records are in there?
然后找出里面有多少条记录?
回答by Quassnoi
Is there a way to do:
SELECT * FROM MY_ITEMS
and then find out how many records are in there?
有没有办法做到:
SELECT * FROM MY_ITEMS
然后找出里面有多少条记录?
If you want it to be in this exact order, you can fetch all records on the client and count their number (almost all client libraries provide a function for that).
如果您希望它按此确切顺序排列,您可以获取客户端上的所有记录并计算它们的数量(几乎所有客户端库都为此提供了一个函数)。
You can also do:
你也可以这样做:
SELECT i.*, COUNT(*) OVER ()
FROM my_items i
, which will return you the count along with each record.
,这将返回计数以及每条记录。
回答by Jim Hudson
If you're working in PL/SQL, you can use the SQL%ROWCOUNT pseudo-variable to get the number of rows affected by the last SQL statement. Might save you some effort.
如果您在 PL/SQL 中工作,您可以使用 SQL%ROWCOUNT 伪变量来获取受最后一条 SQL 语句影响的行数。可能会为您节省一些精力。
回答by EvilTeach
This ought to do the trick.
这应该可以解决问题。
WITH
base AS
(
SELECT *
FROM MY_ITEMS
)
SELECT (SELECT COUNT(*) FROM base) kount,
base.*
FROM base
回答by Will Marcouiller
I'm just unsure about the table aliases, I don't remember in Oracle if they require 'AS' or not. But this should work.
我只是不确定表别名,我不记得在 Oracle 中它们是否需要“AS”。但这应该有效。
select mt.*, c.Cntr
from MyTable mt
, (select COUNT(*) as Cntr
from MyTable
) c