Jump to content

Archived

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

Kingskin

Count number of times data appears

Recommended Posts

Hi,

 

I'd like to be able to count the number of times the same data appears in a table. I know i could just use a standard COUNT to do it, but the trouble is I want to do it for every item in my table. An example. Say a column from my table called 'country' looks like this:

 

country:

 

Holland

England

Ireland

England

Italy

Holand

England

 

I want to return a list of entries, numbered by the amount of times they occur, like this:

 

England - 3

Holland - 2

Ireland - 1

Italy - 1

 

Can anyone help me with this please?

Share this post


Link to post
Share on other sites

Never mind, sorted now. For anyone else who may turn ths thread up after a search, here's what I did:

 

$query = "SELECT location FROM episode";
$result = mysql_query($query);
while ( $row = mysql_fetch_array($result))
{
extract($row);
$locations[] = $location;
}
$output = array_count_values($locations);

Share this post


Link to post
Share on other sites

FYI..

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] country, COUNT(country) FROM episode GROUP BY country [!--sql2--][/div][!--sql3--]

Share this post


Link to post
Share on other sites
FYI..

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] country, COUNT(country) FROM episode GROUP BY country [!--sql2--][/div][!--sql3--]

315422[/snapback]

 

Ah, Ok that looks like a better way of doing it, thanks :)

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.