search

Home  >  Q&A  >  body text

redis - REIDS 排名问题, 取前后5名

用 zAdd 加入排名

zAdd ranks 1000 老王
zAdd ranks 1001 如花
zAdd ranks 1002 死鱼
....

想取 用户老王及老王的前后各5名, 有没有简便方法?

谢谢。

怪我咯怪我咯2797 days ago790

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-22 09:01:51

    First, get Lao Wang’s current ranking: ZRANK ranks 老王, assuming Lao Wang’s ranking is Nth.

    (The top 5 I am talking about here refer to those with a higher score than Lao Wang, and the bottom 5 are those with a smaller score than Lao Wang)
    Then Lao Wang’s top 5 are: ZRANGE ranks N+1 N+6,
    Lao Wang’s last 5 are: ZRANGE ranks N-6 N-1.

    To put it all together, plus Lao Wang himself, it is: ZRANGE ranks N-6 N+6.

    Note: You must remember to first determine whether N-6 and N-1 are less than 0.

    reply
    0
  • Cancelreply