|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
RE: count and add common row fields
try this...
select category, count(category)as num_cat from mytable group by category |
|
#3
|
|||
|
|||
|
RE: count and add common row fields
I dont get how to echo the results though.
|
|
#4
|
|||
|
|||
|
RE: count and add common row fields
|
|
#5
|
|||
|
|||
|
RE: count and add common row fields
ditto!!!
|
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
RE: count and add common row fields
|
|
#8
|
|||
|
|||
|
RE: count and add common row fields
Perfect...thank you.
|
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > count and add common row fields |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|