MySQL - 一起使用COUNT和LIMIT函数 [英] MySQL - Using COUNT and LIMIT functions together

查看:2116
本文介绍了MySQL - 一起使用COUNT和LIMIT函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含14个条目的表格'Labs'。



查询 SELECT COUNT(Labs.sid)FROM Labs LIMIT 0,18446744073709551615 c>返回14。



但是查询 SELECT COUNT(Labs.sid)FROM Labs LIMIT 1,18446744073709551615 返回0(不是我预期的13)。实际上,当我设置LIMIT行偏移量值2,3,4等时,仍然返回0。



为什么?有没有办法使用只是mysql确定偏移后的行数?



FWIW我使用MySQL 5.5.9和InnoDB数据库引擎。



请尝试此操作。

  SELECT COUNT(sid)FROM 

SELECT sid FROM Labs LIMIT 2,18446744073709551615
)T1

请注意,使用LIMIT没有ORDER BY是一个坏主意,不能保证顺序一致,所以在理论上每次都可以选择不同的行(如果 sid 可以为NULL)。


I have a table 'Labs' with 14 entries.

The query SELECT COUNT(Labs.sid) FROM Labs LIMIT 0, 18446744073709551615 returns "14".

However the query SELECT COUNT(Labs.sid) FROM Labs LIMIT 1, 18446744073709551615 returns "0" (not 13 as I would expect). In fact, "0" is still returned when I set the LIMIT row offset value 2, 3, 4, etc.

Why is this? Is there a way to determine the number of rows after a specified offset using just mysql?

FWIW I'm using MySQL 5.5.9 and the InnoDB database engine.

解决方案

The limit is applied AFTER the count is performed.

Try this instead:

SELECT COUNT(sid) FROM
(
    SELECT sid FROM Labs LIMIT 2, 18446744073709551615
) T1

Note that it is a bad idea to use LIMIT without an ORDER BY because you can't guarantee that the order will be consistent so different rows could in theory be selected each time (which matters if sid can be NULL).

这篇关于MySQL - 一起使用COUNT和LIMIT函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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