问题描述:

suppose I have a table t and table t has 15000 entries

suppose the query

`SELECT * FROM t WHERE t.nid <1000`

returns 1000 rows

but then I only want the first 10 rows so I do a LIMIT

`SELECT * FROM t WHERE t.nid <1000 LIMIT 10`

is it possible to construct a single query in which in addition to returning the 10 rows information with the LIMIT clause above, it also returns the total count of the rows that satisfy the conditions set in the WHERE clause, hence in addition to returning the 10 rows above, it also returns 1000 since there are a total of 1000 rows satisfying the WHERE clause...and have both returned in a single query

You can try `SQL_CALC_FOUND_ROWS`

, which can get a count of total records without running the statement again.

```
SELECT SQL_CALC_FOUND_ROWS * FROM t WHERE t.nid <1000 LIMIT 10; -- get records
SELECT FOUND_ROWS(); -- get count
```

Reference: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html

the fastest way to do it is using SQL_CALC_FOUND_ROWS and two queries

```
SELECT SQL_CALC_FOUND_ROWS * FROM t WHERE t.nid <1000 LIMIT 10
SELECT FOUND_ROWS()
```

You can, but I think it would be a performance killer.

Your best option would be to use the `SQL_CALC_FOUND_ROWS`

clause and issue a second query to recover that.

```
SELECT SQL_CALC_FOUND_ROWS * FROM t WHERE t.nid <1000 LIMIT 10;
SELECT FOUND_ROWS();
```

See e.g http://www.arraystudio.com/as-workshop/mysql-get-total-number-of-rows-when-using-limit.html

Or you could simply run the query without `LIMIT`

clause, and retrieve only the first ten rows. Then you can use one query as you wanted, and also get row count through `mysql_num_rows()`

.

Sounds like you want FOUND_ROWS()

```
SELECT SQL_CALC_FOUND_ROWS * FROM t WHERE t.nid <1000 LIMIT 10;
SELECT FOUND_ROWS();
```