Jump to content

Count number of times data appears


Kingskin

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?

Link to comment
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);

Link to comment
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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.