how to know how many times its a value in the databes? and for all values?
This can be aplied to tags, to coments, ...
My question its to obta开发者_Go百科in for example, top 20 repetitions for an atribute in a table..
I mean, i know i can ask for a selected atribute with the mysql_num_rows, but how do i do it to know all of them?
For example,
Most popular colors: Red -100000 blue -5000 white -200 and so on...
if anyone can give me a clue.. thanks!
SELECT `name`, count(*) as `count` FROM `colors`
GROUP BY `name`
ORDER BY `count` DESC
You want to do this computation on the database, not in your application. Usually, a query of the following form should be fine:
SELECT color_id, COUNT(product_id) AS number
FROM products
GROUP BY color_id
ORDER BY number DESC
LIMIT 20
It will be faster this way, as only the value-count data will be sent from the database to the application. Also, if you have indices set up correctly (on color_id
, for instance), thighs will be smoother.
精彩评论