title: mysql scalar function with for loop
body: <p>i have the table 'points_level' with :</p>
  
  <pre><code>id | description | level
  --------------------------
  1  | noob        | 0
  2  | rookie      | 50
  3  | The boss    | 100
  </code></pre>
  
  <p>i need to build a function that return the id of the level giving points as integer for example :</p>
  
  <pre><code>select calc_level(12)
  result: 1
  
  select calc_level(90)
  result: 2
  
  select calc_level(300)
  result: 3
  
  select calc_level(100)
  result: 3
  
  select calc_level(-50)
  result: 1
  </code></pre>
  
  <p>i need to add some level in the points_level table (in the near future) so i shuldn't calculate the results via some simple "CASE WHEN" i think about a "CASE WHEN" in a for loop, i found some example for SSQL but nothing for MYSQL and i don't know the MYSQL SYNTAX for create this kind of function :(</p>
  
  <p>somebody can help me ?</p>
  
  <p>Thanks</p>