Dennis
Dennis

Reputation: 87

Is there a similar count method in SQL like mysql_num_row

I am using SQL and I would like to count the number of rows without a loop. I can't seem to get the "COUNT" keyword working but I know that in MySQL mysql_num_row does the trick. Is there a similar method to mysql_num_row in SQL?

Upvotes: 3

Views: 215

Answers (2)

RichardTheKiwi
RichardTheKiwi

Reputation: 107766

Given ANY query

select ..
from .. multiple ..
where .. group by.. order by..
limit ..

Wrap it in a subquery, and COUNT(*) over it

select count(*) from (

select ..
from .. multiple ..
where .. group by.. order by..
limit ..

) X

The ) X bit is to give it an alias name, as required by syntax. Of course, if your original query ended with a ;, drop it from the subquery.

Upvotes: 2

das_weezul
das_weezul

Reputation: 6142

SELECT COUNT(*) FROM tablename

This will count the number of rows/entries in the table "tablename"

For more info, check out the manual: http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html

[EDIT] As Ken White suggested you can of course narrow down the resulting recordset by adding a WHERE-clause

Upvotes: 3

Related Questions