简体   繁体   English

MySQL:选择一个优胜者,返回他们的排名

[英]MySQL: SELECT a Winner, returning their rank

Earlier I asked this question , which basically asked how to list 10 winners in a table with many winners, according to their points. 早些时候我问了这个问题该问题基本上是询问如何在表中列出10个获胜者,根据他们的观点。

This was answered. 回答了。

Now I'm looking to search for a given winner X in the table, and find out what position he is in, when the table is ordered by points. 现在,我想在表格中搜索给定的获胜者X,并找出按点对表格进行排序时他所处的位置

For example, if this is the table: 例如,如果这是表:

     Winners:
NAME:____|__POINTS:
Winner1  |  1241
Winner2  |  1199
Sally    |  1000
Winner4  |  900
Winner5  |  889
Winner6  |  700
Winner7  |  667
Jacob    |  623
Winner9  |  622
Winner10 |  605
Winner11 |  600
Winner12 |  586
Thomas   |  455
Pamela   |  434
Winner15 |  411
Winner16 |  410

These are possible inputs and outputs for what I want to do: 这些是我想做的可能的输入和输出:

Query:  "Sally", "Winner12", "Pamela", "Jacob"
Output: 3        12          14        623

How can I do this? 我怎样才能做到这一点? Is it possible, using only a MySQL statement? 是否可以仅使用MySQL语句? Or do I need PHP as well? 还是我也需要PHP?

This is the kind of thing I want: 这是我想要的东西:

WHEREIS FROM Winners WHERE Name='Sally' LIMIT 1

Ideas? 有想法吗?

Edit - NOTE: You do not have to deal with the situation where two Winners have the same Points (assume for simplicity's sake that this does not happen). 编辑-注意:您不必处理两个优胜者拥有相同积分的情况(为简单起见,假设不会发生这种情况)。

I think this will get you the desired result. 我认为这将为您带来预期的结果。 Note that i properly handles cases where the targeted winner is tied for points with another winner. 请注意,我会正确处理目标获胜者与另一获胜者并列积分的情况。 (Both get the same postion). (两者的位置相同)。

SELECT COUNT(*) + 1 AS Position
FROM myTable
WHERE Points > (SELECT Points FROM myTable WHERE Winner = 'Sally')

Edit : 编辑
I'd like to "plug" Ignacio Vazquez-Abrams ' answer which, in several ways, is better than the above. 我想“插上” 伊格纳西奥·巴斯克斯(Ignacio Vazquez-Abrams)的答案,从几个方面来说,这比上面的要好。
For example, it allows listing all (or several) winners and their current position. 例如,它允许列出所有(或几个)获奖者及其当前位置。
Another advantage is that it allows expressing a more complicated condition to indicate that a given player is ahead of another (see below). 另一个优点是,它允许表达更复杂的条件以指示给定玩家在另一个玩家之前(参见下文)。 Reading incrediman 's comment to the effect that there will not be "ties" prompted me to look into this; 阅读incrediman关于不会有“联系”的评论,促使我对此进行了研究。 the query can be slightly modified as follow to handle the situation when players have same number of points (such players would formerly have been given the same Position value, now the position value is further tied to their relative Start values). 可以对查询进行如下修改,以处理玩家拥有相同点数时的情况(以前为这些玩家提供了相同的位置值,现在位置值进一步与其相对的起始值相关联)。

SELECT w1.name, (
  SELECT COUNT(*)
  FROM winners AS w2
  WHERE (w2.points > w1.points) 
     OR (W2.points = W1.points AND W2.Start < W1.Start)  -- Extra cond. to avoid ties.
)+1 AS rank
FROM winners AS w1
-- WHERE W1.name = 'Sally'   -- optional where clause
SELECT w1.name, (
  SELECT COUNT(*)
  FROM winners AS w2
  WHERE w2.points > w1.points
)+1 AS rank
FROM winners AS w1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM