gpt4 book ai didi

php - mySQL 中 FOUND_ROWS() 的错误结果

转载 作者:可可西里 更新时间:2023-11-01 07:54:36 25 4
gpt4 key购买 nike

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

或者在一个 block 中:

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

最佳答案

编辑:

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

所以在你的查询中:

选择 sql_calc_found_rows .....

关于php - mySQL 中 FOUND_ROWS() 的错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5955471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com