Jump to content


Photo

Select Count


  • Please log in to reply
3 replies to this topic

#1 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 25 September 2006 - 11:22 PM

The statement Select Count(*) FROM Table. How do you get a count for certain records in a table? ie what would the syntax be for eg Towns. Thanks 

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 September 2006 - 11:32 PM

SELECT COUNT(Towns) AS towncount FROM tbl;


#3 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 25 September 2006 - 11:37 PM

From thorpe's example, the PHP variable $towncount would have the number you're looking for after the query is run.

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 26 September 2006 - 12:52 AM

From thorpe's example, the PHP variable $towncount would have the number you're looking for after the query is run.

actually, you'd have to extract the result from the query:
<?php
$sql = mysql_query("SELECT COUNT(Towns) AS towncount FROM tbl");
$count = mysql_result($sql, 0, 'towncount');
echo $count;
?>

another thought: if you're wanting to just count the DISTINCT towns, you may be able to just do something like this (although, i'm not sure if it's any more efficient):
<?php
$sql = mysql_query("SELECT DISTINCT(Towns) FROM tbl");
$towncount = mysql_num_rows($sql);
echo $towncount;
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users