user327712
user327712

Reputation: 3321

SQL select statement

I got a Table which has two fields: Point, and Level,
with some sample data as follows:

-----------------------
Point | Level
-----------------------
10 | Level 1
20 | Level 2
30 | Level 3
40 | Level 4

Suppose that there is a user who has 25 points,
to find the Level in which this user is in, the Select statement I used was:

Select Level from Table where Point < 30 AND Point > 20;

But the Select SQL ststament is a hard-copy one
where you can see the ponts 30 and 20 are fixed.
I want to alter the Select statement so that the new SQL Select
statement can be applied to all users with different points,
but I don't know how to do it.

Upvotes: 1

Views: 231

Answers (2)

True Soft
True Soft

Reputation: 8796

SELECT Level FROM Table WHERE Point<=25 ORDER BY POINT DESC LIMIT 1 

Or

SELECT Level FROM Table WHERE Point>=25 ORDER BY POINT LIMIT 1 

Depends on what level the user has for 25 points (2 or 3).

Upvotes: 5

adopilot
adopilot

Reputation: 4500

select
   case 
      when point between 20 and 30 then 30
      /*optionally You can add more Cases */
      /*when some other then again other */
      else point
   end as point
from Table 

Upvotes: 0

Related Questions