我正在使用mysql构建基于PHP的搜索。
但我遇到了这个问题: 我有一个很长很重的(在mysql服务器上)查询搜索。
要对结果进行分页,我需要以下数据:
计算所有结果(不限,所以我知道要显示多少个琐事)
SELECT COUNT(*) as count FROM table WHERE col="val"
结果有限 - 我向用户展示
SELECT * FROM table WHERE col="val" LIMIT 20, 10
如何避免对上述2项要求进行2次单独查询?
注意: 为了避免"你的查询有多重?",这是我真实的条件:
SELECT *,
(1000 * 6371 * acos( cos( radians(42.6051862) ) * cos( radians( project_lat ) ) * cos( radians( project_lng ) - radians(23.037836800000036) ) + sin( radians(42.6051862) ) * sin( radians( project_lat ) ) ) ) AS distance
FROM `projects`
JOIN `subcategories` ON `projects`.`subcat_id` = `subcategories`.`subcat_id`
JOIN `categories` ON `subcategories`.`cat_id` = `categories`.`cat_id`
JOIN `project_images` ON `projects`.`project_id` = `project_images`.`project_id`
WHERE `projects`.`subcat_id` = 618
AND `project_start` < '2016-10-30'
AND `project_start` > '2016-10-14'
AND `project_price` > 123
AND `project_price` < 233
AND `project_currency` = 'BTN'
AND `project_approved` = 1
AND MATCH (project_title, project_description) AGAINST ('test')
GROUP BY `projects`.`project_id`
HAVING `distance` < 7400
答案 0 :(得分:0)
使用sql_calc_found_rows
SELECT SQL_CALC_FOUND_ROWS * FROM table WHERE id > 100 LIMIT 10;
SELECT FOUND_ROWS();