这是我的原始查询:
.py
它也有效并返回预期结果。现在我想在限制之前计算总行数。这是我的新查询似乎有效。我的意思是我不确定它是否正常工作,但是测试说它运作正常:
SELECT u.id user_id,
u.avatar,
u.date_time,
CONCAT(u.user_fname, ' ', u.user_lname) name,
sum($this->table_alias.vote_value) vote_value,
sum($this->table_alias.score) score,
$category_in_the_select AS tc $tag_in_the_select AS tt
FROM users u
JOIN reputations $this->table_alias ON u.id = $this->table_alias.owner_id $query_join
WHERE $time_limitation $query_where
GROUP BY user_id, u.avatar, u.date_time, name, tc, tt
ORDER BY score DESC, vote_value DESC
LIMIT :j, $this->per_page
正如您所看到的,我在应用SELECT x.*, @cnt cnt
FROM
(SELECT (@cnt := @cnt + 1) total,
u.id user_id,
u.avatar,
u.date_time,
CONCAT(u.user_fname, ' ', u.user_lname) name,
sum($this->table_alias.vote_value) vote_value,
sum($this->table_alias.score) score,
$category_in_the_select AS tc $tag_in_the_select AS tt
FROM users u
CROSS JOIN
(SELECT @cnt := 0) params
JOIN reputations $this->table_alias ON u.id = $this->table_alias.owner_id $query_join
WHERE $time_limitation $query_where
GROUP BY user_id, u.avatar, u.date_time, name, tc, tt
ORDER BY total) x
ORDER BY x.score DESC, x.vote_value DESC
LIMIT :j, $this->per_page
子句之前使用变量来计算结果。
注意到,我的方法比LIMIT
函数快两倍。无论如何,我想知道的是,我的方法是正确的吗?或者没有任何保证,我不能依赖它?
再次,我测试了它,显然工作正常。