Reputation: 87
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
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
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