tags:

views:

77

answers:

2

Hi all,

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.

A: 
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 
adopilot
+5  A: 
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).

True Soft
shouldn't it be SELECT Level FROM Table WHERE Point <= 25 ORDER BY POINT DESC LIMIT 1?
Maxem
@Maxem, You are right, I updated the answer. I tested it now.
True Soft
-1 the answer should not be coded to 25 as a hardcoded value.
Randy
@Randy: It isn't hardcoded. In that place, he can put his variable from the program. It would be hardcoded if it contained 20 or 30.
True Soft