现在我有
select data.* from
complicated select with 100 joins
as data
我希望它是
select data.*, count(*) as total from
complicated select with 100 joins
as data
但第二个不起作用,它只返回一个带有总数的结果。
我想要的是返回所有结果(包含所有字段),以及每个结果中一个字段的总数。
result.field1 = XX result.field2 = XX ..... result.total = 1000000,
答案 0 :(得分:0)
您必须进行2次查询:
1)是SQL_CALC_FOUND_ROWS
的查询:
SELECT SQL_CALC_FOUND_ROWS data.* FROM
complicated select with 100 joins
as data;
2)是计算找到的行:
SELECT FOUND_ROWS() AS count;
更详细here
奖励:如果您正在使用Sequelise ORM您必须知道您可以使用缩短逻辑的模型和findAndCount
方法。请阅读this article