如何从mysql查询中获取检索到的文档/对象总数?

0yg35tkg  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(278)

我有个问题: SELECT SQL_CALC_FOUND_ROWS DISTINCT something.* FROM someting WHERE TRUE AND something.is_active=1 ORDER BY long_id DESC . 上面的查询将检索一些对象和文档。
在检索文档/对象本身之前,是否可以从该查询中获取文档/对象的总数?
这里的情况是表“something”可以有超过100000个文档/对象,因此不能由php处理(返回致命错误内存不足)。因此,我想知道将从查询中检索多少文档/对象。
我试过 COUNT :

SELECT COUNT(*) SQL_CALC_FOUND_ROWS DISTINCT something.* FROM something WHERE TRUE AND something.is_active=1 ORDER BY long_id DESC
SELECT COUNT(SQL_CALC_FOUND_ROWS DISTINCT something.*) FROM something WHERE TRUE AND something.is_active=1 ORDER BY long_id DESC
SELECT SQL_CALC_FOUND_ROWS DISTINCT COUNT(something.*) FROM something WHERE TRUE AND something.is_active=1 ORDER BY long_id DESC

但总有这样的错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ... .

nfs0ujit

nfs0ujit1#

SELECT COUNT(DISTINCT something.*) FROM something WHERE something.is_active=1 ORDER BY long_id DESC

既然你用了 COUNT ,无需使用 SQL_CALC_FOUND_ROWS . 另外,我删除 TRUE 因为它是不必要的。

roejwanj

roejwanj2#

SELECT COUNT(*) FROM something WHERE something.is_active=1

只需简化查询并计算所有结果。 DISTINCT something.* 也没有意义(除非您的表没有主键,我对此表示怀疑)。

相关问题