joyacv2
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?
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?
I think you can use a GROUP BY with COUNT()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
/* 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