Jump to content

List and count values in a table


simonp

Recommended Posts

Hi,

 

I thought what I was trying to do was simple but appears to be a little beyond my mysql knowledge ;)

 

I have a table (populated by a drop down box on a web page) which looks a bit like:

 

Dave

Will

Simon

Andy

Will

Simon

Dave

Dave

Chris

Dave

 

... and I need to be able to query the number of times each name appears (extra names may be added in the future to can't hard code the names).

 

So I should end up with

 

Dave - 4

Will - 2

Simon - 2

Andy - 1

 

So far I have:

 

$query = "SELECT * FROM table WHERE `fieldid` = 1 ORDER BY value ASC";
$result=mysql_query($query);
while($data = mysql_fetch_array($result)) {
$value = $data["value"];
$uses = $data["uses"];
$reportdata["tablevalues"][] = array($value,$uses);
}

 

...which just lists all the name.

 

I think I need a foreach and maybe a count (*) but am not having much joy - any help appeciated!

 

 

Simon

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.