我有以下查询:SELECT SQL_CALC_FOUND_ROWS DISTINCT something.* FROM someting WHERE TRUE AND something.is_active=1 ORDER BY long_id DESC
。上面的查询将检索一些对象和文档。
能否在检索文档/对象本身之前从该查询中获取文档/对象的总数?
这里的情况是表“某物”可以具有超过100,000个文档/ objetcs,因此不能由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 ...
。
答案 0 :(得分:0)
SELECT COUNT(*) FROM something WHERE something.is_active=1
只需简化您的查询并计算所有结果。 DISTINCT something.*
也没有任何意义(除非您的表没有主键,我怀疑是这样)。
答案 1 :(得分:0)
SELECT COUNT(DISTINCT something.*) FROM something WHERE something.is_active=1 ORDER BY long_id DESC
由于您使用COUNT
,因此无需使用SQL_CALC_FOUND_ROWS
。另外,我在where子句中删除了TRUE
,因为它是不必要的。