Link to home
Start Free TrialLog in
Avatar of joyacv2
joyacv2Flag for Puerto Rico

asked on

SQL and php to count rows and in a table and summarize in another table

Hi,

I have this table

cars
dogs
cars
dogs
cats
tables
electronics
tables
tables
electronics

and so on...

I need a query and  php code to give a table that presents the unique values and count how many exists of each of them, like a pivot table in excel

cars              2
dogs             2
cats              1
tables           3
electronics  2  

any idea?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I think you can use a GROUP BY with COUNT()
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joyacv2

ASKER

works perfect!!!!!
Thanks for the points.  Here is line 117 deconstructed:

/* Get me the column named fname and the count of the id column.  Name the count "num" in the results set */
SELECT fname, COUNT(id) as num FROM my_table

/* Group all matching fname columns together, and order the results set alphabetically */
GROUP BY fname ORDER BY fname


Best regards, ~Ray