简体   繁体   中英

How to find a range from two columns in mysql

I have a value say 45.

I want to check whether this value is present in database or not.

The problem is that I have two columns, say:

--------------------
range_from  range_to
--------------------
   10 ------------ 20
--------------------
   21 ------------ 30
--------------------
   31 ------------ 40
--------------------
   41 ------------ 50
--------------------

How can I find this value?

SELECT * FROM Score WHERE range_from='' AND range_to=''
SELECT *
FROM score
WHERE range_from < 45 AND range_to > 45;

Normally you would use the keyword 'BETWEEN'

SELECT * FROM score 
WHERE 45 BETWEEN range_from AND range_to

To take care of boundary conditions also we can consider small correction in the above answer

SELECT *
FROM score
WHERE range_from <= 50 AND range_to >= 50;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM