-1

Is there a way that I can get the COUNT(*) of what a query will return? For example:

SELECT * FROM table LIMIT 10 // Query
SELECT (*) FROM table LIMIT 10 // Query Count

This would actually ignore the limit ( MySQL COUNT with LIMIT). While this might be fine and 'correct' within sql, I need the exact number of rows the query is returning. How would this be done?

Community
  • 1
  • 1
David542
  • 104,438
  • 178
  • 489
  • 842
  • Did you consider `SELECT MIN(COUNT(pk), 10) FROM table WHERE column = 'value'`? (this would obviously *not* work with offsets) – h2ooooooo Jan 13 '16 at 19:51
  • Use `SQL_CALC_FOUND_ROWS` and `FOUND_ROW()`: http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_found-rows. – Gordon Linoff Jan 13 '16 at 20:11

1 Answers1

0

if you actually want to accommodate limit, you can use:

select count(*) from (SELECT * FROM table LIMIT 10) as t
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • Thanks, is there a more performant way to do the above than executing the subquery on SELECT * ? – David542 Jan 13 '16 at 19:49
  • @David542 I cannot tell for sure whether mysql will optimize it or not, but it make sense to select only primary key column instead of `*` – Iłya Bursov Jan 13 '16 at 19:50