skip to Main Content

2 days ago I started to learn redis, and now I have problem. I need to limit sorted set’s return value. In the context of my task, I need to specify the city and the price for a ticket, and I need to get the 3 most expensive and 3 cheapest flights, but I cannot limit the return values, but I’ve made new Sorted Set called one, and added there 1 – one; 2 – two. I can’t figure out what the command syntax should be.

I’ve tried:

zrange one 0 5 [LIMIT 1]
(error) ERR syntax error
127.0.0.1:6379> zrange one 0 5 LIMIT 1
(error) ERR syntax error
127.0.0.1:6379> zrange one 0 5 [1]
(error) ERR syntax error

and documentation didn’t help me…

2

Answers


  1. the LIMIT syntax should follow two params, the first is offset, the second is count.
    you can try the follow command for you test.

    zrange one 0 5 LIMIT 0 1
    

    the offset is 0, the count is 1, means get the first result from zrange one 0 5

    Login or Signup to reply.
  2. You’re trying to query by rank. While querying by rank, you don’t need to (and you can’t) provide LIMIT options. Because you’re already providing the limit by the ranks. For example, if you want to query between rank 5 and 10, your limit equals (10-5+1)=6.


    Coming to your query, in ascending order, lowest 3 ranks are between 0 and 2. So the query would be:

    zrange one 0 2
    

    Again, in ascending order, highest 3 ranks are between -3 and -1. So the query would be:

    zrange one -3 -1
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search