2

我正在尝试向查询全文索引表的结果添加分页。这是查询:

普通查询

SELECT *,MATCH(title) AGAINST ("+samsung +galaxy +s3" IN BOOLEAN MODE) 
as score FROM `deals` WHERE `image`!='' AND category=15032 ORDER BY score DESC;

它返回183行。

尝试进行分页

SELECT SQL_CALC_FOUND_ROWS *,MATCH(title) AGAINST 
("+samsung +galaxy +s3" IN BOOLEAN MODE) as score FROM `deals` 
WHERE `image`!='' AND category=15032 ORDER BY score DESC LIMIT 8;

按预期返回 8 行,因为我想每页显示 8 个项目。

接着

SELECT FOUND_ROWS();

它返回20项目,而不是183原始查询中的项目。我不确定是什么原因造成的。你能帮我解决这个问题吗?

谢谢。

4

2 回答 2

0

你做错了,做分页你需要两个限制,开始和结束,看看这个例子并将限制正确添加到你的代码中

mysql> select * from t1 order by actor_id;
+----------+-------------+--------------+---------------------+
| actor_id | first_name  | last_name    | last_update         |
+----------+-------------+--------------+---------------------+
|        1 | PENELOPE    | GUINESS      | 2006-02-15 04:34:33 |
|        2 | NICK        | WAHLBERG     | 2006-02-15 04:34:33 |
|        3 | ED          | CHASE        | 2006-02-15 04:34:33 |
|        4 | JENNIFER    | DAVIS        | 2006-02-15 04:34:33 |
|        5 | JOHNNY      | LOLLOBRIGIDA | 2006-02-15 04:34:33 |
|        6 | BETTE       | NICHOLSON    | 2006-02-15 04:34:33 |
|        7 | GRACE       | MOSTEL       | 2006-02-15 04:34:33 |
|        8 | MATTHEW     | JOHANSSON    | 2006-02-15 04:34:33 |
|        9 | JOE         | SWANK        | 2006-02-15 04:34:33 |
|       10 | CHRISTIAN   | GABLE        | 2006-02-15 04:34:33 |
|       11 | ZERO        | CAGE         | 2006-02-15 04:34:33 |
|       12 | KARL        | BERRY        | 2006-02-15 04:34:33 |
|       13 | UMA         | WOOD         | 2006-02-15 04:34:33 |
|       14 | VIVIEN      | BERGEN       | 2006-02-15 04:34:33 |
|       15 | CUBA        | OLIVIER      | 2006-02-15 04:34:33 |
|       16 | FRED        | COSTNER      | 2006-02-15 04:34:33 |
|       17 | HELEN       | VOIGHT       | 2006-02-15 04:34:33 |
|       18 | DAN         | TORN         | 2006-02-15 04:34:33 |
|       19 | BOB         | FAWCETT      | 2006-02-15 04:34:33 |
|       20 | LUCILLE     | TRACY        | 2006-02-15 04:34:33 |
....

+----------+-------------+--------------+---------------------+
201 rows in set (0.00 sec)

现在你应用限制,开始和结束

mysql> select * from t1 order by actor_id limit 0,5;
+----------+------------+--------------+---------------------+
| actor_id | first_name | last_name    | last_update         |
+----------+------------+--------------+---------------------+
|        1 | PENELOPE   | GUINESS      | 2006-02-15 04:34:33 |
|        2 | NICK       | WAHLBERG     | 2006-02-15 04:34:33 |
|        3 | ED         | CHASE        | 2006-02-15 04:34:33 |
|        4 | JENNIFER   | DAVIS        | 2006-02-15 04:34:33 |
|        5 | JOHNNY     | LOLLOBRIGIDA | 2006-02-15 04:34:33 |
+----------+------------+--------------+---------------------+
5 rows in set (0.00 sec)

mysql> select * from t1 order by actor_id limit 6,5;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update         |
+----------+------------+-----------+---------------------+
|        7 | GRACE      | MOSTEL    | 2006-02-15 04:34:33 |
|        8 | MATTHEW    | JOHANSSON | 2006-02-15 04:34:33 |
|        9 | JOE        | SWANK     | 2006-02-15 04:34:33 |
|       10 | CHRISTIAN  | GABLE     | 2006-02-15 04:34:33 |
|       11 | ZERO       | CAGE      | 2006-02-15 04:34:33 |
+----------+------------+-----------+---------------------+
5 rows in set (0.00 sec)

mysql> select * from t1 order by actor_id limit 11,5;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update         |
+----------+------------+-----------+---------------------+
|       12 | KARL       | BERRY     | 2006-02-15 04:34:33 |
|       13 | UMA        | WOOD      | 2006-02-15 04:34:33 |
|       14 | VIVIEN     | BERGEN    | 2006-02-15 04:34:33 |
|       15 | CUBA       | OLIVIER   | 2006-02-15 04:34:33 |
|       16 | FRED       | COSTNER   | 2006-02-15 04:34:33 |
+----------+------------+-----------+---------------------+
5 rows in set (0.00 sec)

所以这个想法是你使用LIMIT $begin,$number_results的,当你点击下一步时,它将把 number_result + total_result_per_page 放在 $begin. 假设您在第 3 页,每页显示 10 个结果。($begin=10+10+10=30)

选择......哪里......订购......LIMIT 30,10

电子数据交换:

found_rows 将从表的开头返回到您的限制,

 mysql> select * from t2 order by actor_id limit 10;
+----------+------------+--------------+---------------------+
| actor_id | first_name | last_name    | last_update         |
+----------+------------+--------------+---------------------+
|        1 | PENELOPE   | GUINESS      | 2006-02-15 04:34:33 |
|        2 | NICK       | WAHLBERG     | 2006-02-15 04:34:33 |
|        3 | ED         | CHASE        | 2006-02-15 04:34:33 |
|        4 | JENNIFER   | DAVIS        | 2006-02-15 04:34:33 |
|        5 | JOHNNY     | LOLLOBRIGIDA | 2006-02-15 04:34:33 |
|        6 | BETTE      | NICHOLSON    | 2006-02-15 04:34:33 |
|        7 | GRACE      | MOSTEL       | 2006-02-15 04:34:33 |
|        8 | MATTHEW    | JOHANSSON    | 2006-02-15 04:34:33 |
|        9 | JOE        | SWANK        | 2006-02-15 04:34:33 |
|       10 | CHRISTIAN  | GABLE        | 2006-02-15 04:34:33 |
+----------+------------+--------------+---------------------+
10 rows in set (0.00 sec)

mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
|           10 |
+--------------+
1 row in set (0.00 sec)

mysql> select * from t2 order by actor_id limit 2,10;
+----------+------------+--------------+---------------------+
| actor_id | first_name | last_name    | last_update         |
+----------+------------+--------------+---------------------+
|        3 | ED         | CHASE        | 2006-02-15 04:34:33 |
|        4 | JENNIFER   | DAVIS        | 2006-02-15 04:34:33 |
|        5 | JOHNNY     | LOLLOBRIGIDA | 2006-02-15 04:34:33 |
|        6 | BETTE      | NICHOLSON    | 2006-02-15 04:34:33 |
|        7 | GRACE      | MOSTEL       | 2006-02-15 04:34:33 |
|        8 | MATTHEW    | JOHANSSON    | 2006-02-15 04:34:33 |
|        9 | JOE        | SWANK        | 2006-02-15 04:34:33 |
|       10 | CHRISTIAN  | GABLE        | 2006-02-15 04:34:33 |
|       11 | ZERO       | CAGE         | 2006-02-15 04:34:33 |
|       12 | KARL       | BERRY        | 2006-02-15 04:34:33 |
+----------+------------+--------------+---------------------+
10 rows in set (0.00 sec)

mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
|           12 |
+--------------+
1 row in set (0.00 sec)

它是 12,因为显示了 10 个结果 + id 1 和 2 + 10 个显示的结果

于 2012-06-21T12:27:38.200 回答
0

尝试使用HAVING而不是WHERE

SELECT SQL_CALC_FOUND_ROWS 
    *, 
    MATCH(title) AGAINST ("+samsung +galaxy +s3" IN BOOLEAN MODE) as score 
FROM `deals` 
HAVING`image`!='' 
    AND category=15032 
ORDER BY score DESC 
LIMIT 8;
于 2014-08-25T11:45:20.237 回答