Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old July 23rd, 2003, 08:40 PM
mugane mugane is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Charlottesville, VA USA
Posts: 425 mugane User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to mugane Send a message via Yahoo to mugane
how to count distinct elements, MySQL

Hi,

My problem has to do with counting records in a table. I would like to end up with an associative array $count, where each index in $count is the field value and each respective value in $count is the number of occurrences of that value in the table.

To clarify, say I have a field "category", whose value must be one of several options (say "one", "two", or "three"). I do not know how many records there are in the table, there could be any number. Also, there could be any number of options for the "category" field, so a dynamic approach is necessary.

This is what I have so far:
php Code:
Original - php Code
  1. <?
  2. $query = "SELECT count(*) FROM tablename";
  3. $result = mysql_fetch_array(mysql_query($query));
  4. $count = $result[0]; // this just gets the total number of records
  5. ?>
I also know that using "SELECT count(DISTINCT category) FROM tablename" will yield the total number of distinct categories, but will not reveal the number of occurrences of each distinct category.

If anyone has any ideas about how to generate this information in an efficient manner please let me know.

Thanks,

Peter

Reply With Quote
  #2  
Old July 23rd, 2003, 09:28 PM
brut brut is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 367 brut User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 30 sec
Reputation Power: 2
RE: how to count distinct elements, MySQL

This query should work:
Code:
SELECT DISTINCT category,COUNT(category) AS cat_num FROM tablename GROUP BY category;

Reply With Quote
  #3  
Old July 23rd, 2003, 11:43 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
RE: how to count distinct elements, MySQL

brut's query should work, but there's more in there than you need (DISTINCT + GROUP BY is a little redundant). This is simplier:

Code:
SELECT category, COUNT(*) AS cat_num FROM tablename GROUP BY category

Reply With Quote
  #4  
Old July 24th, 2003, 12:09 PM
mugane mugane is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Charlottesville, VA USA
Posts: 425 mugane User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to mugane Send a message via Yahoo to mugane
RE: how to count distinct elements, MySQL

Thanks a heap for the replies, much appreciated.

This is what I ended up with:
php Code:
Original - php Code
  1. <?
  2. $query = "SELECT columnname,count(*) AS col_num FROM tablename GROUP BY columnname ASC";
  3. $result = mysql_query($query);
  4. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  5. while(list($key, $value) = each($row))
  6. {
  7. if($key!="col_num") $name = $value;
  8. else $count[$name]=$value;
  9. }
  10. }
  11. while(list($key, $value) = each($count)) echo "$key = $value<br>";
  12. ?>
echoes:
Code:
computersgeneral = 4
computershardware = 2

This works, but I think it looks horribly inefficient (two returned values in $result where only one is necessary) - do you know a better way to do it (e.g. a query that returns an associative array with [index == the column value] and [value == number of occurrences of column value])?

Thank you very much,

Peter

Reply With Quote
  #5  
Old July 24th, 2003, 01:14 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
RE: how to count distinct elements, MySQL

$result is not an array, it's a mysql result identifier. You need to use mysql_fetch_array to get the next row of data from it.

php Code:
Original - php Code
  1.  
  2. $query = "SELECT category, COUNT(*) AS cat_num FROM tablename GROUP BY category";
  3. $result = mysql_fetch_array(mysql_query($query),MYSQL_ASSOC)  ;
  4. while($row = mysql_fetch_array($result))
  5. {
  6. echo "$row[category] = $row[cat_num]<br>";
  7. }

Reply With Quote
  #6  
Old July 24th, 2003, 01:16 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
RE: how to count distinct elements, MySQL

Ignore most what what I just posted. I must be drinking.

You need to repeatedly use mysql_fetch_array. The way you had it originally, you just got the first row returned.

php Code:
Original - php Code
  1.  
  2. $query = "SELECT category, COUNT(*) AS cat_num FROM tablename GROUP BY category";
  3. $result = mysql_query($query);
  4. while($row = mysql_fetch_array($result))
  5. {
  6. echo "$row[category] = $row[cat_num]<br>";
  7. }

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > how to count distinct elements, MySQL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT