{"id":5559,"date":"2012-05-17T17:21:11","date_gmt":"2012-05-17T11:51:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=5559"},"modified":"2017-05-10T16:53:29","modified_gmt":"2017-05-10T11:23:29","slug":"get-totalcount-of-records-when-using-limit-in-one-query","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/get-totalcount-of-records-when-using-limit-in-one-query\/","title":{"rendered":"get totalCount of records when using limit in one Query"},"content":{"rendered":"<p>For pagination we generally execute two query ,first for geting resultset by using limit in sql query and second to count the total no. of records . For total no. of records we again execute the same query with count(*).<\/p>\n<p>You would need two queries like these:<\/p>\n<p>[java]<br \/>\nSELECT COUNT(*) FROM author WHERE name LIKE &#8216;a%&#8217;;<\/p>\n<p>SELECT name, email FROM author WHERE name LIKE &#8216;a%&#8217; LIMIT 10;<br \/>\n[\/java]<\/p>\n<p>But if you have a complex query that joins several tables and takes a while to execute \u2013 well, you probably wouldn\u2019t want to execute it twice and waste server resources.<\/p>\n<p>Since MYSQL 4.0 we can use<strong> SQL_CALC_FOUND_ROWS<\/strong> option in\u00a0 query which will tell MySQL to count total number of rows <strong><em>disregarding LIMIT clause<\/em><\/strong>. In main query add <strong>SQL_CALC_FOUND_ROWS<\/strong> option just after SELECT and in second query\u00a0 use <strong>FOUND_ROWS()<\/strong> function to get total number of rows without executing the query.<\/p>\n<p>Queries would look like this:<\/p>\n<p>[java]<br \/>\nSELECT SQL_CALC_FOUND_ROWS name, email FROM author WHERE name LIKE &#8216;a%&#8217; LIMIT 10;<\/p>\n<p>SELECT FOUND_ROWS();<br \/>\n[\/java]<\/p>\n<p><strong>Limitation:<\/strong> Must call second query immediately after the first one(or before next one) because SQL_CALC_FOUND_ROWS does not save number of rows anywhere.In the absence of the SQL_CALC_FOUND_ROWS option in the most recent successful SELECT statement, FOUND_ROWS() returns the number of rows in the result set returned by that statement.<br \/>\n<!--more--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For pagination we generally execute two query ,first for geting resultset by using limit in sql query and second to count the total no. of records . For total no. of records we again execute the same query with count(*). You would need two queries like these: [java] SELECT COUNT(*) FROM author WHERE name LIKE [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":16},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5559"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=5559"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5559\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=5559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=5559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=5559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}