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 November 26th, 2003, 01:17 AM
mdhall mdhall is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 158 mdhall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
count and add common row fields

Ok, lets say I have a table with 50 rows in it. All rows have the same fields, and each row has a field named "category". The category contains the value/data of either Cat_1, Cat_2, or Cat_3.

I want to run a query that will find how many rows contain each value, add them, and display the results, for example...

Cat_1 has 22 rows
Cat 2 has 17 rows
Cat_3 has 11 rows

I'm not sure how to set up a COUNT or loop to do this.

Reply With Quote
  #2  
Old November 26th, 2003, 02:20 AM
Blindeddie Blindeddie is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: NJ - USA
Posts: 2,152 Blindeddie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
RE: count and add common row fields

try this...

select category, count(category)as num_cat from mytable group by category




Reply With Quote
  #3  
Old November 26th, 2003, 02:40 AM
mdhall mdhall is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 158 mdhall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: count and add common row fields

I dont get how to echo the results though.

Reply With Quote
  #4  
Old November 26th, 2003, 02:50 AM
nazly nazly is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Colombo,SriLanka
Posts: 1,325 nazly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 18 sec
Reputation Power: 3
Send a message via Yahoo to nazly
RE: count and add common row fields

Adding more to blindeddie's reply to solve your problem..

php Code:
Original - php Code
  1.  
  2. $query="select category, count(category)as num_cat from mytable group by category";
  3. $result=mysql_result($query);
  4. while($row=mysql_fetch_array($result)){
  5.  echo $row["category"]." has ".$row["num_cat"]." rows<br>";
  6. }

Reply With Quote
  #5  
Old November 26th, 2003, 02:53 AM
Blindeddie Blindeddie is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: NJ - USA
Posts: 2,152 Blindeddie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
RE: count and add common row fields

ditto!!!

Reply With Quote
  #6  
Old November 26th, 2003, 03:03 AM
mdhall mdhall is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 158 mdhall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: count and add common row fields

O, my category name is actually recipe_category, so I changed the query to this...

$query="select recipe_category, count(recipe_category)as num_cat from recipes group by recipe_category";
$result=mysql_result($query);
while($row=mysql_fetch_array($result)){
echo $row["recipe_category"]." has ".$row["num_cat"]." rows<br>";
}

recipes is the table name. I'm not getting anything to display.

Reply With Quote
  #7  
Old November 26th, 2003, 03:09 AM
nazly nazly is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Colombo,SriLanka
Posts: 1,325 nazly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 18 sec
Reputation Power: 3
Send a message via Yahoo to nazly
RE: count and add common row fields

Oops.. I have made a small mistake.. I have used mysql_result() instead of mysql_query().. So Sorry..

php Code:
Original - php Code
  1.  
  2. $query="select recipe_category, count(recipe_category)as num_cat from recipes group by recipe_category";
  3. $result=mysql_query($query);
  4. while($row=mysql_fetch_array($result)){
  5. echo $row["recipe_category"]." has ".$row["num_cat"]." rows<br>";
  6. }

Reply With Quote
  #8  
Old November 26th, 2003, 03:12 AM
mdhall mdhall is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 158 mdhall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: count and add common row fields

Perfect...thank you.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > count and add common row fields


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 6 hosted by Hostway