I have a query that works
$query_list_records = "SELECT COUNT(*)
FROM (SELECT comment_id from myl_r_comments
WHERE release_id=? LIMIT ? OFFSET ?) A";
This does COUNT all rows from the 2nd SELECT.
But for optimisation purposes,
why does the following line not work?
$query_list_records = "SELECT COUNT(*)
FROM myl_r_comments
WHERE release_id=? LIMIT ? OFFSET ?";
I figure, for a lot number of rows, returning only the COUNT value is nicer than returning a lot of rows (one column selected) and then counting them.
Let me know what you think.
In the first version, the
LIMITis applied and then theCOUNT(*)is taken of the result: e.g. the subquery returns X records, which are limited to Y, andCOUNT(*)in the outer query returns Y.In the second version, the
COUNT(*)of all the records is taken and then theLIMITis applied (but there’s only one, aggregated record to limit): so X is the sole record returned (but theOFFSETmight cause it to be excluded from the resultset).You could instead do: