3

我总共有 6 行。当我做一个查询(比如 SELECT * from table)并且我有

  • LIMIT 3 => FOUND_ROWS() 给出 3 => 检索到 3 行
  • LIMIT 1, 3 => FOUND_ROWS() 给出 4 => 检索到 3 行
  • LIMIT 2, 3 => FOUND_ROWS() 给出 5 => 检索到 3 行
  • LIMIT 3, 3 => FOUND_ROWS() 给出 6 => 检索到 3 行
  • LIMIT 4, 3 => FOUND_ROWS() 给出 6 => 检索到 2 行

知道这种奇怪行为的原因是什么吗?

SQL查询

SELECT `places`.*, `category`.*, COUNT(places_reviews.place_id) AS num_reviews, (places_popularity.rating_1 + 2*places_popularity.rating_2 + 3*places_popularity.rating_3 + 4*places_popularity.rating_4 + 5*places_popularity.rating_5)/(places_popularity.rating_1 + places_popularity.rating_2 + places_popularity.rating_3 + places_popularity.rating_4 + places_popularity.rating_5) AS average_rating, FOUND_ROWS() AS num_rows FROM (`places`) JOIN `category` ON `places`.`category_id` = `category`.`category_id` LEFT JOIN `places_reviews` ON `places_reviews`.`place_id` = `places`.`id` LEFT JOIN `places_popularity` ON `places_popularity`.`place_id` = `places`.`id` WHERE `places`.`category_id` = 1 AND `places`.`name` LIKE '%%' GROUP BY `places`.`id` ORDER BY `id` desc LIMIT 3

或者在一个块中:

SELECT `places`.*, `category`.*, 
COUNT(places_reviews.place_id) AS num_reviews, 
(places_popularity.rating_1 + 2*places_popularity.rating_2 + 3*places_popularity.rating_3 + 4*places_popularity.rating_4 + 5*places_popularity.rating_5)/(places_popularity.rating_1 + places_popularity.rating_2 + places_popularity.rating_3 + places_popularity.rating_4 + places_popularity.rating_5) AS average_rating, FOUND_ROWS() AS num_rows FROM (`places`) 
JOIN `category` ON `places`.`category_id` = `category`.`category_id` 
LEFT JOIN `places_reviews` ON `places_reviews`.`place_id` = `places`.`id` 
LEFT JOIN `places_popularity` ON `places_popularity`.`place_id` = `places`.`id` 
WHERE `places`.`category_id` = 1 
    AND `places`.`name` LIKE '%%' 
GROUP BY `places`.`id` 
ORDER BY `id` desc LIMIT 3
4

3 回答 3

5

编辑 :

这就是您要查找的内容: http: //dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

所以在您的查询中:

选择 sql_calc_found_rows .....

于 2011-05-10T19:41:52.637 回答
2

以这种方式尝试

SELECT sql_calc_found_rows `places`.*, `category`.*, 
COUNT(places_reviews.place_id) AS num_reviews, 
(places_popularity.rating_1 + 2*places_popularity.rating_2 + 3*places_popularity.rating_3 + 4*places_popularity.rating_4 + 5*places_popularity.rating_5)/(places_popularity.rating_1 + places_popularity.rating_2 + places_popularity.rating_3 + places_popularity.rating_4 + places_popularity.rating_5) AS average_rating FROM (`places`) 
JOIN `category` ON `places`.`category_id` = `category`.`category_id` 
LEFT JOIN `places_reviews` ON `places_reviews`.`place_id` = `places`.`id` 
LEFT JOIN `places_popularity` ON `places_popularity`.`place_id` = `places`.`id` 
WHERE `places`.`category_id` = 1 
    AND `places`.`name` LIKE '%%' 
GROUP BY `places`.`id` 
ORDER BY `id` desc LIMIT 3;

select found_rows();
于 2011-05-10T19:50:41.870 回答
1

最快的解决方案是像这样子查询您的实际查询:

SELECT SQL_CALC_FOUND_ROWS * FROM (SELECT whatever FROM whatever WHERE whatever LIMIT whatever) ax; 
select FOUND_ROWS();

现在你会得到正确的结果。主要原因是SQL_CALC_FOUND_ROWS主要跟踪找到的行(即没有LIMITS)而不是返回的行。

于 2015-05-19T10:54:07.633 回答