Ranking Database Entries in MYSQL
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]
SE开发者_如何学运维T @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.
SELECT laptop FROM table
GROUP BY laptop
ORDER BY COUNT(laptop) DESC
Why won't that work?
精彩评论