如何在不运行两个单独的进程调用的情况下获取 DBI 中的行计数?
我在 Perl 中运行 DBI,无法弄清楚当我运行准备好的语句时,我如何能够确定返回的行计数是否为 0。
我意识到我可以在 while 循环中设置一个计数器,在其中获取行,但我希望有一种不那么难看的方法来做到这一点。
I'm running DBI in Perl and can't figure out how, when I run a prepared statement, I can figure out if the returned row count is 0.
I realize I can set a counter inside my while loop where I fetch my rows, but I was hoping there was a less ugly way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
快速查看此处,似乎在运行之后
您可以通过以下方式访问行数
Based on a quick look here, it seems that after you run
you can access the row count via
文档中的“警告”(链接到另一个答案)很重要,并提供真实、正确的答案:
The "caveat" in the documentation (linked to in a comment on another answer) is important, and provides the real, correct answer:
为了找出结果集中有多少行,您有两个选择:
select count(*)
您可以通过返回数组或更奇特的东西的存储过程引入一些魔法,但最终需要发生这两件事之一。
所以,没有什么花哨的方法可以得到这个结果。 你只需要数一下它们就可以了:-)
In order to find out how many rows are in a result set you have exactly two options:
select count(*)
You can introduce some magic via a stored procedure that returns an array or something more fancy, but ultimately one of those two things will need to happen.
So, there is no fancypants way to get that result. You just have to count them :-)
虽然有点晚了,但如果有人使用 ORACLE,那么解决方案就来了:
$sql 当然是你的查询。 Oracle 的优化器足够智能,不会执行所有操作两次。
现在,每个获取的行都在 DB_ROWNUM 中保存当前行号(对于分页网格行编号很有用),并在 DB_COUNT 中保存完整的行数。 您仍然必须获取至少一行(因此这并不完全是上面问题的答案;)),但是接下来是汗水使用:
这也是在 Oracle 中进行启动和限制的一种非常简单的方法,并且仍然可以获得完整的行数:
这样,您实际上只能获取网格中第二页的第 51 到 100 行,但在每次获取中仍然具有实际行号(从 1 开始)和完整计数(没有开始和限制)排。
It's a bit late, but if anyone uses ORACLE, here comes THE sweat solution:
$sql is your query, of course. Oracles optimizer is intelligent enough not to execute everything twice.
Now every fetched row holds the current row number (useful for paging grid row numbering) in DB_ROWNUM and the complete number of rows in DB_COUNT. You still have to fetch at least one row (so it isn't exactly the answer to the question above ;)), but the sweat use comes next:
It's also a very easy way to do start and limit in Oracle and still get the complete number of rows:
With this, you can actually fetch only row 51 to 100 for the second page in your grid, but still have the real row number (starting from 1) and the complete count (without start and limit) in every fetched row.
尝试这个 SQL 解决方案,将数据的 SQL 与 count 语句结合起来。
第一个语句将有计数,并且应该是第一行(如果不是,您可以通过排序来绕过它),然后您可以在闲暇时循环浏览记录集的其余部分。
try this SQL solution, combine your SQL for the data with a count Statement.
the first statement will have the count, and should be the first row (if not you can order by to get around it) then you can cycle through the rest of the recordset at your leisure.
CPAN 说:
所以做这样的事情可能会起作用:
CPAN says:
So doing something like this will probably work:
我已经动态生成了 SQL 并执行它。 对我来说,
select count(*)
似乎不是一个选项,因为我必须再次重新生成查询。 以下方法对我来说看起来很干净。 但是您必须再次发出$h->execute()
才能检索行数据。--
沙昆塔拉
I have dynamically generated a SQL and executing it. For me
select count(*)
does not seem to be an option because I have to re-generate the query again. Following approach looked clean for me. But you have to issue s$h->execute()
once again to retrieve row data.--
Shaakunthala
行肯定会根据数据库/驱动程序版本的不同而有所不同。 我肯定会寻找处理意外结果的逻辑。
Rows definitely does vary depending on the database/driver version it seems. I would definitely look to a piece of logic there that dealt with unexpected results.
如果您想在遍历所有行之前知道有多少行,则特定于 MySQL 的解决方案可能是 FOUND_ROWS()。
在第一个查询中,在
SELECT
之后添加SQL_CALC_FOUND_ROWS
。 然后执行SELECT FOUND_ROWS();
,您就可以立即访问行数。 现在您可以决定是要遍历所有行,还是最好的方式。请注意,查询中包含
LIMIT
将为您提供没有LIMIT
时查询返回的总数。If you want to know how many rows are there before walking through all of them, a MySQL-specific solution could be FOUND_ROWS().
In your first query, add
SQL_CALC_FOUND_ROWS
right afterSELECT
. Then doSELECT FOUND_ROWS();
, and you have access to the row count right away. Now you can decide whether you want to walk through all the rows, or the best way to do it.Note that having
LIMIT
in the query will give you the total number query would have returned withoutLIMIT
.正如其他人已经说过的,您确实需要获取行来找出是否有行。 如果您需要在每一行开始循环之前知道,并且预期结果不是很大,则可以将所有结果放入一个数组中,然后进行检查。
我最近使用了类似的东西:
As others already said, you really need to get the rows to find out if there are any. If you need to know before starting a loop on every row, and if the expected results are not huge, you can get all results into an array, and then check that.
I recently used something like that:
我只是让数据库来进行计数。 如果您想要的只是行数。
查找 2018 年 1 月的所有行。
I'd just let the database do the counting instead. If all you want is the number of rows.
Find all the rows for january 2018.
我认为使用参考文献可以很容易地实现这一点。
下面的代码显示了如何不需要重新执行查询来在计数后获取行。
我希望这能满足这里提出的问题。
I think this can be achieved pretty easily using references.
The code blow below shows how it is not needed to go for re-execution of the query to fetch the rows after a count.
I hope this satisfied the question made here.