How many in table?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

How many in table?

Post by Dale »

Ok, now i know this must be a basic command, but how do i get it to print the amount of rows in a table? (eg; dale_houses)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

SELECT COUNT(*) AS `total` FROM `table`
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Justa tiny question, are there any differences in speed count()-ing all the fields (*), or just one field? That is, if all rows has the field set.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

probably, but that would depend on how many columns are in the table and whether the column you count has been indexed.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

from what i have read in the past

the difference between mysql_num_rows + the count method is very little except on large tables where count is recommended

personally i use num_rows method on small tables and count on large tables

also, num_rows requires less lines of code to retrieve the value...

hope that helps
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

Seems like a select count(*) from sometable would be optimized to
use a primary key index and be quicker....guess it depends...
fv
Post Reply