Jump to content

Archived

This topic is now archived and is closed to further replies.

cyprus

Select Count

Recommended Posts

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

Share this post


Link to post
Share on other sites
[code]
SELECT COUNT(Towns) AS towncount FROM tbl;
[/code]

Share this post


Link to post
Share on other sites
From thorpe's example, the PHP variable $towncount would have the number you're looking for after the query is run.

Share this post


Link to post
Share on other sites
[quote author=thepip3r link=topic=109485.msg441381#msg441381 date=1159227435]
From thorpe's example, the PHP variable $towncount would have the number you're looking for after the query is run.
[/quote]
actually, you'd have to extract the result from the query:
[code]
<?php
$sql = mysql_query("SELECT COUNT(Towns) AS towncount FROM tbl");
$count = mysql_result($sql, 0, 'towncount');
echo $count;
?>
[/code]

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):
[code]
<?php
$sql = mysql_query("SELECT DISTINCT(Towns) FROM tbl");
$towncount = mysql_num_rows($sql);
echo $towncount;
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.