在 MySQL 查询中使用 SELECT 和 LIMIT 时如何计算所有行?

2021-11-20 00:00:00 mysql

我有一个像这样的 mysql 查询:

I've got a mysql query like this:

SELECT A.ID, A.NAME, B.ID, B.NAME
FROM table1 A
JOIN table2 B ON ( A.ID = B.TABLE1_ID )
WHERE
    cond1, cond2, ..., condN
LIMIT 10

我在查询中有很多 where 子句.如何改进此查询以获得完整的行数?我不想在没有 LIMIT 的情况下再使用一个请求.

I've got many where clauses in query. How to improve this query to get also full row count? I don't want to use one more request without LIMIT.

推荐答案

您正在寻找的是 这个

SELECT SQL_CALC_FOUND_ROWS A.ID, A.NAME, B.ID, B.NAME
FROM table1 A
JOIN table2 B ON ( A.ID = B.TABLE1_ID )
WHERE
  cond1, cond2, ..., condN
LIMIT 10

SELECT FOUND_ROWS();

相关文章