开发者

How to query to get rank of a row?

开发者 https://www.devze.com 2023-02-17 12:57 出处:网络
I am creating a photo ranking system. Basically, I have three main fields - won, lost, howmanyplayed.

I am creating a photo ranking system. Basically, I have three main fields - won, lost, howmanyplayed.

Let's say I have three photos

Photo  Won  Lost  Played
-------------------------
A      3    2     5
B      1    4     5
C      3    2     5

I am going to write a query to get rank of each photos. The problem is, Photo A and Photo C have exactly same record. How do make a query that returns 1 for both Photo A and C?

Photo  Won  Lost  Played  Rank
-------------------------------开发者_如何学编程
A      3    2     5       1
C      3    2     5       1
B      1    4     5       ?

Added on 3/17/11

I have changed the table structure a little bit

so now I have "vote_ratio" field instead of "played". "vote_ratio" holds value of "vote_win" / "vote_lose".

so the table must be revised as

Photo   Won   Lost   Ratio
A       3     2      1.5 
B       1     4      0.25 
C       3     2      1.5

What I want to do :

  1. query only "Photo A" and get the rank of it.
  2. query all the records and display the rank of each record.

I can do #2 by the following query.

"select * from table order by ratio desc"

but there is a problem again. Photo A and C have same ratio. I need to make both of them rank #1.


if you mean "return 1" as return 1 record of ranking ? -> not tested, however it could combination of GROUP BY and GROUP_CONCAT like this:

SELECT Won, Lost, Played, GROUP_CONCAT( photo, ', ' ) AS photos
FROM your_table
GROUP BY Won, Lost, Played


not tested

SELECT *
FROM photos
ORDER BY (played/lost)

I think it returns :

Photo A // rank1
Photo C // rank1
Photo B // rank2
0

精彩评论

暂无评论...
验证码 换一张
取 消