Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select query with a number range in Mysql

Tags:

sql

mysql

I have a user table like this:

User (id, name, rank)

I want to check user's rank and get the corresponding text (ex: if user's rank in between 1-10, I can get the text "User in top 10!")

I created a rank_text table like this:

rank_text(id, rank_from, rank_to, text)
         (1,  1,         10,      "You are in top 10!")

How I can query this case?

Can you give me a advise?

Thank you very much!

like image 405
Tomato Avatar asked Nov 23 '25 02:11

Tomato


1 Answers

You can use a join:

select u.*, rt.text
from users u left join
     rank_text rt
     on u.rank between rt.rank_from and rt.rank_to;
like image 113
Gordon Linoff Avatar answered Nov 25 '25 17:11

Gordon Linoff