2

I have this relation:

╔═══════════════════╗
║ i++ name  score   ║
╠═══════════════════╣
║ 1   Will  123     ║
║ 2   Joe   100     ║
║ 3   Bill  99      ║
║ 4   Max   89      ║
║ 5   Jan   43      ║
║ 6   Susi  42      ║
║ 7   Chris 11      ║
║ 8   Noa   9       ║
║ 9   Sisi  4       ║
╚═══════════════════╝

with this sql:

set @username = ?;
set @uuid = ?;
SELECT * FROM (
    -- Get those who scored worse (or tied)
    (    SELECT s.*
           FROM quiz.score s
     CROSS JOIN (SELECT points FROM quiz.score WHERE username = @username) and uuid=@uuid ref
          WHERE s.points <= ref.points AND username <> @username
       ORDER BY s.points DESC
          LIMIT 2)
    UNION
    -- Get our reference point record
    (SELECT s.* FROM quiz.score s WHERE username = @username and uuid=@uuid)
    UNION
    -- Get those who scored better
    (    SELECT s.*
           FROM quiz.score s
     CROSS JOIN (SELECT points FROM quiz.score WHERE username = @username and uuid=@uuid) ref
          WHERE s.points > ref.points AND username <> @username
       ORDER BY s.points ASC
          LIMIT 2)
) slice
ORDER BY points ASC;

I got this result

╔═══════════════════╗
║ id++ name score   ║
╠═══════════════════╣
║ 1   Bill  99      ║
║ 2   Max   89      ║
║ 3   Jan   43      ║
║ 4   Susi  42      ║
║ 5   Chris 11      ║
╚═══════════════════╝

but I need that result:

╔═══════════════════╗
║ id++ name score   ║
╠═══════════════════╣
║ 3   Bill  99      ║
║ 4   Max   89      ║
║ 5   Jan   43      ║
║ 6   Susi  42      ║
║ 7   Chris 11      ║
╚═══════════════════╝

I need an iterator column thats count the absolute position of the founded rows in the whole table. Do you know how I can handle this?

4

2 回答 2

1

这将比您的查询运行得快得多:

SELECT @rankOfSearch := count(*) FROM t
WHERE score >= (
  SELECT score FROM t
  WHERE id = 5
)
ORDER BY score DESC;

SELECT id, name, score FROM (
  SELECT t.*, @rank := @rank + 1 rank
  FROM t, (SELECT @rank := 0) init
  ORDER BY score DESC
) s
WHERE rank BETWEEN @rankOfSearch - 2 AND @rankOfSearch + 2
ORDER BY rank

输出:

| ID |  NAME | SCORE |
|----|-------|-------|
|  3 |  Bill |    99 |
|  4 |   Max |    89 |
|  5 |   Jan |    43 |
|  6 |  Susi |    42 |
|  7 | Chris |    11 |

只需确保替换id = 5为您当前拥有(或将来可能拥有)的“中间”记录的任何查找方式。

于 2013-10-10T20:48:32.627 回答
0

如果分数总是按照与 ID 相关的降序排列,您可以使用以下命令:

SET middle = 5
SET max = middle + 2;
SET min = middle - 2;

SELECT *
FROM quiz_score
WHERE id BETWEEN min AND max
于 2013-10-10T20:57:39.983 回答