troubleshooting Question

MySql Query question

Avatar of shwekhaw
shwekhaw asked on
PHPMySQL Server
4 Comments1 Solution213 ViewsLast Modified:
First I am newbie to php and mysql. I am trying to do read some data from a table in most efficient way possible.
Here is a simple example of an table. Two columns are color and username
blue,user1
red,user2
yellow, user3
red,user4
blue,user5
red,user6

The array I want to get out of query is like this
red,3
blue,2
yellow,1

Normally, I would make
1. Select query for distinct color and get array fo colors and save in array A
2. Another query to get count for each color and save in array B
3. Then sort the array using array_multisort so that colors with the most username records is on the top (in above example order would be red,blue,yellow)

Can you get all three steps done in one query? If not, can you get step one and step two done in one query so array will be like (color,# of users) for all distinct colors.

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros