user2183216
user2183216

Reputation: 359

SQL select between 2 values price

I have a value of 15.

I then have a database which contains different ranks for the player.

They have the following information in my ranks table:

Level       CountFrom     CountTo
Level 1       0              9
Level 2       10             19
Level 3       20             29

How can i pull the level out in a sql command?

I.e i have:

SELECT *
FROM `ranks
WHERE `CountTo` <= '15'

This will pull Level 1 and Level 2 as result.

The only way i can think of is to then OrderBy CountTo DESC and limit the result to 1.

Is there a better way?

Upvotes: 0

Views: 2127

Answers (2)

Chamal
Chamal

Reputation: 1449

You can use BETWEEN

SELECT *
FROM `ranks
WHERE '15' between `CountTFrom` and `CountTo`

Upvotes: 2

Daniel Gimenez
Daniel Gimenez

Reputation: 20494

You can use CountFrom in the query as well.

SELECT *
FROM ranks
WHERE CountFrom <= 15 AND CountTo >= 15
  • Level 1 will be excluded because its CountTo(9) value makes CountTo >= 15 false.
  • Level 2 will be included because its CountFrom(10) is true for CountFrom <= 15 and its CountTo(19) is true for CountTo >= 15.
  • Level 3 will be excluded because its CountFrom(20) values makes CountFrom <= 15 false.

Upvotes: 4

Related Questions