views:

72

answers:

4

I have a table, called Level.

id  |  level  |  points(minimum)  
-------------------------
1   |  1      |  0 
2   |  2      |  100 
3   |  3      |  200

Let say I have 189 points, how do i check which level the user in?

EDIT:

Best answer chosen. Now I am comparing the request by adding EXPLAIN before the SELECT query, i have this result:

id  |  select_type  |  table  |  type  |  possible_keys  |  key  |  key_len  |  ref  |  rows  |  Extra
-------------------------------------------------------------------------------------------------------------
1   |    SIMPLE     |  level  |   ALL  |       NULL      |  NULL |    NULL   |  NULL |  8  |  Using where


id  |  select_type  |  table  |  type  |  possible_keys  |  key  |  key_len  |  ref  |  rows  |  Extra
-------------------------------------------------------------------------------------------------------------
1   |    SIMPLE     |  level  |   ALL  |       NULL      |  NULL |    NULL   |  NULL |  8  |  Using where; Using filesort

How do i know which one is better or faster?

A: 

I'm not quite sure what you mean but I think this is what you want:

SELECT `level` FROM `Level` WHERE `points`=189
Dumb Guy
yeah, you got me wrong. i didnt mean like this, but thanks anyway for trying to help me :)
mysqllearner
+6  A: 

If you're looking for the level that the player is currently in, you want to select the maximum level with a points requirement less than the points the player currently has:

select max(level) from level where points <= 189;

This may work better if each level has a min_points and max_points amount:

id | level | min_points | max_points
------------------------------------
1  |   1   | 0          | 99
2  |   2   | 100        | 199
3  |   3   | 200        | 299

Then your query wouldn't need to aggregate:

select * from level where min_points <= 189 && max_points > 189;

Edit: ugh, I keep messing up my SQL tonight, LOL.

Kaleb Brasee
I was thinking about the max/min columns too, but the setup the OP lists is best. There's no concern with one range overlapping another, and the max/min is an exponential increase in columns based on the number of levels.
OMG Ponies
Sorry, not exponential, but multiplication
OMG Ponies
@matt: Seems like your, matt and Madcoder answer are working fine. Now I am wondering, which method/query is better/faster?? Please advice. Even if it faster in few milliseconds
mysqllearner
SOrry, i mean, i accidentally added "@matt", please ignore it..
mysqllearner
weird, i use this: select max(level) from level where points <= 189;I try 245 or 189, the result is: level 1. Why??
mysqllearner
I think i know why. The level column is varchar type, not int, so i cant use max() right?
mysqllearner
The actual value of level column is "noobie", "normal", "expert" etc not 1,2,3..
mysqllearner
Not sure... I created the table and am getting level 3 for 245 and level 2 for 189.
Kaleb Brasee
Oh... then you'd need to select max(id) instead.
Kaleb Brasee
I tested too - works fine, assuming level is an integer. I thought it might've had something to do with level appearing to be a MySQL keyword, but that's not the case.
OMG Ponies
Yeah, it works. Thanks Kaleb :)
mysqllearner
+2  A: 

This wouldn't require a 'between' or any kind of aggregate function at all.. you could just select all rows that are less than the points, sort descending, and then first row should be the correct one.

select level from Level where points <= 189 order by points desc limit 1

(Assuming MySQL .. if you do not have MySQL, 'limit' may not work)

Matt
A: 
select max(level) from level where points <= 189

This assumes the 'points' field in the table is the mininum points ot achieve that level.

MadCoder