Ranking Database Entries in MYSQL
Question
I'm trying to rank the entries in my database, based on the number of times a user saves that term.
So if my table is ,
id laptop Price 1 Macbook $2000 2 MBP $2300 3 MBP $2300 4 MBP $2000
The query will return
1.) MBP 2.) Macbook
I foolishly thought ranking GROUP_BY and DESC would do this, then I tried the @Rank := @Rank +1 approach to no avail. [I must admit, i'm not sure if this is the right way to solve the problem]
SET @rank :=0; SELECT * FROM ( SELECT d.*, @rank := @rank + 1 FROM data d ORDER BY d.`laptop` ) d2 WHERE d2.laptop = 'MBP'
Thanks for any assistance.
Solution
SELECT laptop FROM table
GROUP BY laptop
ORDER BY COUNT(laptop) DESC
Why won't that work?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow