我总共有 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