查询中有LIMIT时,如何获取总结果数? [英] How to get the number of total results when there is LIMIT in query?

查看:406
本文介绍了查询中有LIMIT时,如何获取总结果数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的查询:

select * from table where id <= 10 limit 5;  // table has +10 rows

上述查询^中的结果数为10行.现在我想知道,如何获取此查询中的总结果数:

The number of result in the above query ^ is 10 rows. Now I want to know, how can I get the number of total result in this query:

select * from table where col = 'anything' limit 5;

如何计算此^中所有结果的数量(与limit无关)?

How to calculate the number of all results (regardless of limit) in this ^ ?

实际上我想要这个电话号码:

Actually I want this number:

select count(*) as total_number from table where col = 'anything'

现在我想知道如何在不进行其他查询的情况下获得总结果数.

Now I want to know how can I get the number of total results without another query.

推荐答案

添加列total,例如:

select t.*
     , (select count(*) from tbl where col = t.col) as total
from tbl t
where t.col = 'anything'
limit 5

@Tim Biegeleisen 所述: limit关键字在所有其他内容之后应用,因此count(*)仍返回正确答案.

As stated by @Tim Biegeleisen: limit keyword is applied after everything else, so the count(*) still returns the right answer.

这篇关于查询中有LIMIT时,如何获取总结果数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆