Jump to content

Methods to Count number of rows in a SELECT


Recommended Posts

I know how to do this, but my question is I have come across 3 ways.


First is


SELECT count(*)



Second is




Third is


$result = mysql_query(...)





So what is the norm? What is best practice these days? I know that count(*) is the older method and I think its depreciated,  SQL_CALC_FOUND_ROWS is for version 4 and up I believe.

Link to comment
Share on other sites

there isn't really a norm to it.

You just find the one that works best for you and stick with it.


I personally like to count them at query time...using COUNT(*) as whatever...then I can just access that variable later.


If you know you're gonna need that count a lot in your code I'd recommend using COUNT(*) to save code

if you just need only one time...then use mysql_num_rows()

Link to comment
Share on other sites

SELECT count(*) is fastest


Not always... it really depends what you're trying to accomplish, and have the LIMIT & OFFSET are like.  Sure, it's easier just to write a single query and "cheat" with calc_rows, sometimes it's better and sometimes it's worse... depends on all sorts of things, group bys, having clauses, etc.  No hard and fast rule.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.